MSMVPS.COM
The Ultimate Destination for Blogs by Current and Former Microsoft Most Valuable Professionals.

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 Dec 28 2007, 07:20 AM by gary
Filed under:

Comments

PHil wrote re: Create a web part to show sites and sub-sites
on 12-28-2007 16:23

Hi Gary,

I am a little confused as to which tree view control you are using.  I am not sure where the tree view control is in the .Net Framework.

Could you give a poor guy a hint?

Phil

Marc wrote re: Create a web part to show sites and sub-sites
on 01-13-2008 6:12

Hi!

It's great your control!! Thanks for share with everyone!

bubblez wrote re: Create a web part to show sites and sub-sites
on 01-16-2008 2:39

You say it is security trimmed. What permissions are required to see a node? In the default SPTreeview only site administrators can see the tree...

Add a Comment

(required)  
(optional)
(required)  
Remember Me?


Copyright © is the original authors. Blog site is an independent site not sponsored by Microsoft. The Yoda blog server and the Brianna SQL server would like to thank www.ownwebnow.com and www.exchangedefender.com. They wouldn't be here and broadcasting without the generosity of Vlad Mazek and his companies.

Powered by Community Server (Commercial Edition), by Telligent Systems