Search code examples
csssharepointsharepoint-2010master-pagesweb-parts

Global Permissions-Based Navigation Sharepoint Foundation 2010


I am using SharePoint Foundation 2010 and I would like to create a multilevel navigation. (Similar to what the publishing site allows in SharePoint Standard. I realize that this could probably be built using a custom sitemap provider, which I have looked into.

The kicker here, is that I would like for it only show the pages and sites in which the logged in user has permissions to.

Currently, I have began developing a web part. Inside this web part I have written some c# code to loop through and get all the sites and subpages ( and check if the user has permissions) and then add them to a label. Eventually I would like to add them to either an asp:menu or create a and then use some css or jquery to manipulate it to do the drop down features.

Here is some of my code:

using (SPWeb oWebsite = new SPSite(webUrl).OpenWeb())
        {
            SPWebCollection collWebsite = oWebsite.Webs;

            foreach (SPWeb subSite in collWebsite)
            {
                if (subSite.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser.LoginName, SPBasePermissions.Open))
                {
                    Label1.Text += SPEncode.HtmlEncode(subSite.Title) + "<BR>";
                }

                SPList pagelist = subSite.Lists["Site Pages"];

                foreach (SPListItem item in pagelist.Items)
                {
                    if (item.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser, SPBasePermissions.Open))
                                            {
                        Label1.Text += item.Name + "<BR>";
                    }
                }

                Label1.Text += "<BR><BR>";

                subSite.Close();
            }

            Label1.Text += "<BR><BR><BR><BR>";
        }

    }

Instead of adding the site or page to the label, here is where I would like to create the list. The ultimate goal would be to place this web part onto the masterpage and allow for it to give the users some navigation based on permissions.

I also found some css to allow the multilevel navigation to work by adding elements simply as a

  • inside of an The only issue here is that once I placed this onto the masterpage, The 2nd level displayed when I hovered over the first item, but would disappear upon moving my mouse there. (This worked great when the webpart was placed onto a page itself)

    Am I on the right track here or is there another method that may work just as well or even better?

    TIA.


  • Solution

  •  SPSecurity.RunWithElevatedPrivileges(delegate
              {
    
            SPSite oSiteCollection = SPContext.Current.Site;
    
            using (SPWeb oWebsite = new SPSite(webUrl).OpenWeb())
            {
                SPWebCollection collWebsite = oWebsite.Webs;
    
                foreach (SPWeb subSite in collWebsite)
                {
                    var newItem = new MenuItem();
                    if (subSite.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser.LoginName, SPBasePermissions.Open))
                    {
                        newItem.NavigateUrl = subSite.Url;
                        newItem.Text = SPEncode.HtmlEncode(subSite.Title);
                        newItem.Value = SPEncode.HtmlEncode(subSite.Title);
                    }
    
                    SPList pagelist = subSite.Lists["Site Pages"];
    
                    foreach (SPListItem item in pagelist.Items)
                    {
    
                        if (item.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser, SPBasePermissions.Open))
                        {
                            var subItem = new MenuItem();
                            subItem.Value = SPEncode.HtmlEncode(item.Name);
                            subItem.Text = SPEncode.HtmlEncode(item.Name);
                            subItem.NavigateUrl = item.Url;
                            newItem.ChildItems.Add(subItem);
                        }
    
                    }
    
                    mnNAv.Items.Add(newItem);
                    subSite.Close();
               }
    
            }
    });