ShareBlog

Gary Bushey's Blog

Create a web part to show sites and sub-sites

Saw a newgroup question the other day about how to do this in SharePoint 2007.  While the solution posted works quite well here is some code that I wrote that should accomplish what the user needs.   This will either start at the root of your site collection or a specified location (which may or may not be the site you are on).  It will then add all the sites and sub-sites to a treeview control, security trimmed of course.  Note that it uses recursion to view all the sub-sites so depending on how your system is architected this may take a while.  Guess a simple enhancement would be to add a parameter to limit the depth of your treeview.  You could get even more fancy and show X levels by default and then if you expand beyond that add the nodes dynamically.  Big Smile 

 

using System;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Publishing.Navigation;

namespace GBushey.Portal.SiteMap
{
    [Guid("1e900b56-1f33-4772-b5b9-9df73612c40d")]
    public class PortalSiteMap : System.Web.UI.WebControls.WebParts.WebPart
    {
        string _StartNodeName;
        bool _StartAtTopLevel = false;

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        FriendlyName("Start at the Root Node?")]
        public bool StartAtTopLevel
        {
            get
            {
                return _StartAtTopLevel;
            }
            set
            {
                _StartAtTopLevel = value;
            }
        }

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        FriendlyName("The name of the root level site")]
        public string StartNodeName
        {
            get
            {
                return _StartNodeName;
            }
            set
            {
                _StartNodeName = value;
            }
        }

        protected override void CreateChildControls()
        {
            this.ChromeType = PartChromeType.None;
          
            try
            {
                if (_StartAtTopLevel || _StartNodeName.Length > 0)
                {
                    PortalSiteMapDataSource siteMapDataSource = new PortalSiteMapDataSource();

                    siteMapDataSource.TrimNonCurrentTypes = Microsoft.SharePoint.Publishing.NodeTypes.Page;
                    siteMapDataSource.TrimNonCurrentTypes = Microsoft.SharePoint.Publishing.NodeTypes.List;

                    base.CreateChildControls();
                    PortalSiteMapProvider portalSiteMap = new PortalSiteMapProvider();
                    portalSiteMap.IncludePages = PortalSiteMapProvider.IncludeOption.Never;
                    portalSiteMap.IncludeHeadings = false;
                    portalSiteMap.EncodeOutput = true;

                    SiteMapNode startNode;
                    if (_StartAtTopLevel)
                    {
                        startNode = portalSiteMap.RootNode;
                    }
                    else
                    {
                        startNode = portalSiteMap.FindSiteMapNodeFromKey(_StartNodeName);
                    }
                    SiteMapNodeCollection nodes = portalSiteMap.GetChildNodes(startNode);


                    SPTreeView siteTreeView = new SPTreeView();
                    siteTreeView.ExpandDepth = 10;

                    siteTreeView.Nodes.Add(new TreeNode(startNode.Title, startNode.Url, "", startNode.Url, ""));

                    TreeNode topNode = siteTreeView.Nodes[0];
                    ProcessWeb(topNode, nodes);

                    Controls.Add(siteTreeView);
                }
                else
                {
                    Controls.Clear();
                    Label errorMessage = new Label();
                    errorMessage.Text = "Enable the web part to start either at the root node or at a specific Url";
                    Controls.Add(errorMessage);
                }
            }
            catch (Exception ex)
            {
                Controls.Clear();
                Label errorMessage = new Label();
                errorMessage.Text = "There was an error in the code.  Please contact your system administrator and rely the following " +
                    "message: " + ex.Message;
                Controls.Add(errorMessage);
            }

        }

        private void ProcessWeb(TreeNode topNode, SiteMapNodeCollection nodes)
        {
            try
            {
                foreach (SiteMapNode currentMapNode in nodes)
                {
                        TreeNode currentNode = new TreeNode(currentMapNode.Title, currentMapNode.Url, "", currentMapNode.Url, "");
                        if (currentMapNode.ChildNodes.Count > 0)
                        {
                            ProcessWeb(currentNode, currentMapNode.ChildNodes);
                        }
                        topNode.ChildNodes.Add(currentNode);
                }
            }
            catch (Exception e)
            {
            }

        }
    }
}

Posted: Fri, Dec 28 2007 7:20 by gary | with no comments
Filed under: