Wednesday, June 27, 2012

Create a Visual Web Part that displays the entire SharePoint site structure of a SharePoint web application

In the following code sample, I've created a Visual Web Part that will display the entire site structure contained under the parent SharePoint Web Application.  This web part will display all sites despite the fact that the given user may not have access to all of the sites beneath the structure.  Naturally, unauthorized users won't be able to access the site when he/she clicks on the link, but they will at least be aware of its existence and can contact the SharePoint Admin in order to request access.  For this web part, I've chosen to use a Tree View control for displaying the structure, but feel free to get creative if you like.  By the way, I've also added a method, SetCurrentNodeFont, here that can be used for highlighting the SharePoint site under which the web part is installed.  Last but not least, I've removed my error handling code in order to simplify things a bit so be sure to include or suffer the consequences...  


Here is the code:

ASCX File

<asp:TreeView ID="treeViewSiteNav" runat="server">
</asp:TreeView>


ASCX.CS File

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;

protected void Page_Load(object sender, EventArgs e)
{
    treeViewSiteNav.Nodes.Clear();
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        // Display the entire navigation structure of the web application
        SPSiteCollection sites = SPContext.Current.Site.WebApplication.Sites;
        foreach (SPSite currentSite in sites)
        {
            MapCurrentSiteCollection(currentSite.ID, currentSite.RootWeb.ID);
            currentSite.Dispose();
        }
    });
}


private void MapCurrentSiteCollection(Guid siteId, Guid webId)
{
    // Creating a new instance of the site because the currentSite is associated with the context of the
    // user whereas this new instance will run with elevated privileges
    using (SPSite site = new SPSite(siteId))
    {
        site.CatchAccessDeniedException = false;
        using (SPWeb currentWeb = site.OpenWeb(webId))
        {
            TreeNode siteNode = new TreeNode(currentWeb.Title, null, null, currentWeb.Url, "_self");
            SetCurrentNodeFont(ref siteNode, SPContext.Current.Web.ID, currentWeb.ID);
            treeViewSiteNav.Nodes.Add(siteNode);
            foreach (SPWeb web in currentWeb.Webs)
            {
                AddWebNodes(web, siteNode);
                web.Dispose();
            }
        }
    }
}

private void SetCurrentNodeFont(ref TreeNode node, Guid currentWebId, Guid evaluatedWebId)
{
    if (currentWebId == evaluatedWebId)
    {
        node.Text = Server.HtmlDecode(string.Format("<b>{0}</b>", node.Text));
    }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.