PANVEGA’s Blog

DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

How to retrieve Sites and Pages from the SiteMapProvider recursively

Posted by PANVEGA on April 8, 2009

In this short post I gonna show you how to retreive  a SharePoint SiteMapProvider here CurrentNavSiteMapProvider (LeftNavBar) in your MasterPage and create a XML file where you gonna write the XmlNodes in a hierarchigal structure. You can use the XML for instance to add the control into your MasterPage and parse the Navigation Tree with Silverlight, JavaScript or Flash in order to get your custom animated SharePoint Navigation.

Sharepoint is going to build you a sitemapprovider based off your logical architecture of your site. So, if you site has a root site and it has children and it’s children have children – then the sitemapprovider it creates for you will look like that.

Read more about SiteMapProvider and how to customize in one of my previous post:

https://panvega.wordpress.com/2008/04/18/customize-the-sharepoint-navigation-aspmenu

Provides PortalSiteMapNode objects that represent a merging of the Windows SharePoint Services 3.0 SP Navigation store and dynamic site structure, including sites and pages.


SiteMapProvider siteMapProvider = SiteMap.Providers[SiteMapProviderName];

XmlNode siteNode = null;
        public XmlNode SiteNode
        {
            get { return siteNode; }
            set { siteNode = value; }
        }

//here you check the currentNode a give the 2 paramaters to the recursive method. First you have to create a new XmlDocument where you add the nodes.

SiteMapNode startNode = portalSiteMapProvider.CurrentNode;
                if (!startNode.HasChildNodes)
                    startNode = portalSiteMapProvider.CurrentNode;

                getRecursiveNavigation(startNode, SiteNode);

//in the if statement i check if the node is a page or a site

private void getRecursiveNavigation(SiteMapNode pageNode, XmlNode lastChild)
        {
            foreach (SiteMapNode node in pageNode.ChildNodes)
            {
                XmlNode tmpnode = null;
                if (node.Url.Contains(".aspx"))
                {
                    tmpnode = XmlDoc.CreateElement("Page");
                }
                else
                {
                    tmpnode = XmlDoc.CreateElement("Site");
                }
                XmlAttribute titleAttribute = XmlDoc.CreateAttribute("Title");
                titleAttribute.Value = node.Title;
                tmpnode.Attributes.Append(titleAttribute);
                XmlAttribute urlAttribute = XmlDoc.CreateAttribute("Url");
                urlAttribute.Value = SiteUrl + node.Url;
                tmpnode.Attributes.Append(urlAttribute);
                lastChild.AppendChild(tmpnode);

                if (lastChild.HasChildNodes)
                    getRecursiveNavigation(node, lastChild.LastChild);
            }
        }

//after parsing I added the SiteNode to the XmlDocument

// -- process the site info
            SiteNode = xmlDoc.CreateElement("RootNode");
            XmlAttribute siteTitleAttribute = xmlDoc.CreateAttribute("Title");
            XmlAttribute siteUrlAttribute = xmlDoc.CreateAttribute("Url");
            siteTitleAttribute.Value = portalSiteMapProvider.CurrentWeb.Title;
            siteUrlAttribute.Value = portalSiteMapProvider.CurrentWeb.Url;
            SiteNode.Attributes.Append(siteTitleAttribute);
            SiteNode.Attributes.Append(siteUrlAttribute);
            xmlDoc.AppendChild(SiteNode);

Some recursive output values:

<XML>
    <RootNode Title="Litware Inc." Url="http://moss.litwareinc.com">
        <Page Title="Documents" Url="http://moss.litwareinc.com/_layouts/viewlsts.aspx?BaseType=1">
            <Page Title="Trusted Data Connections" Url="http://moss.litwareinc.com/Trusted Data Connections/Forms/AllItems.aspx"/>
            <Page Title="not finished" Url="http://moss.litwareinc.com/not finished/Forms/AllItems.aspx"/>           
        </Page>
        <Site Title="Backup Test" Url="http://moss.litwareinc.com/export">
            <Page Title="Documents" Url="http://moss.litwareinc.com/export/_layouts/viewlsts.aspx?BaseType=1">
                <Page Title="Shared Documents" Url="http://moss.litwareinc.com/export/Shared Documents/Forms/AllItems.aspx"/>
            </Page>           
            <Page Title="Discussions" Url="http://moss.litwareinc.com/export/_layouts/viewlsts.aspx?BaseType=0&amp;ListTemplate=108">
                <Page Title="Team Discussion" Url="http://moss.litwareinc.com/export/Lists/Team Discussion/AllItems.aspx"/>
            </Page>
            <Page Title="Sites" Url="http://moss.litwareinc.com/export/_layouts/viewlsts.aspx?ShowSites=1"/>
            <Page Title="People and Groups" Url="http://moss.litwareinc.com/export/_layouts/people.aspx"/>
            <Site Title="ThirdLevelSite" Url="http://moss.litwareinc.com/export/ThirdLevelSite">
                <Page Title="Documents" Url="http://moss.litwareinc.com/export/ThirdLevelSite/_layouts/viewlsts.aspx?BaseType=1">
                    <Page Title="Shared Documents" Url="http://moss.litwareinc.com/export/ThirdLevelSite/Shared Documents/Forms/AllItems.aspx"/>
                </Page>               
                <Page Title="Discussions" Url="http://moss.litwareinc.com/export/ThirdLevelSite/_layouts/viewlsts.aspx?BaseType=0&amp;ListTemplate=108">
                    <Page Title="Team Discussion" Url="http://moss.litwareinc.com/export/ThirdLevelSite/Lists/Team Discussion/AllItems.aspx"/>
                </Page>
                <Page Title="Sites" Url="http://moss.litwareinc.com/export/ThirdLevelSite/_layouts/viewlsts.aspx?ShowSites=1"/>
                <Page Title="People and Groups" Url="http://moss.litwareinc.com/export/ThirdLevelSite/_layouts/people.aspx"/>
            </Site>
        </Site>
</RootNode></XML>

Luckily, you can just instantiate the SharePoint sitemapprovder in your own code when you want. Which sitemapprovider to use depends on if you have a team site or a publishing portal (the difference is, if you have a publishing portal you use the portalsitemapprovider). After you have intantiated it in your own code, you can loop through it and do what you want.

Ex – with a portalsitemapprovider:

Code Snippet
 protected PortalSiteMapProvider _portalSiteMapProvider = new PortalSiteMapProvider();
        private void InitializePortalSiteMapProvider()
        {
            _portalSiteMapProvider.IncludeAuthoredLinks = true;
            _portalSiteMapProvider.IncludeHeadings = false;
            _portalSiteMapProvider.IncludePages = PortalSiteMapProvider.IncludeOption.Always;
            _portalSiteMapProvider.IncludeSubSites = PortalSiteMapProvider.IncludeOption.Always;
            _portalSiteMapProvider.NavigationType = PortalNavigationType.Combined;
        }

Here is a list of the SiteMapProviders listed in your Webapplication web.config:

<siteMap defaultProvider="CurrentNavSiteMapProvider" enabled="true">
      <providers>
        <add name="SPNavigationProvider" type="Microsoft.SharePoint.Navigation.SPNavigationProvider, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <add name="SPSiteMapProvider" type="Microsoft.SharePoint.Navigation.SPSiteMapProvider, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <add name="SPContentMapProvider" type="Microsoft.SharePoint.Navigation.SPContentMapProvider, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <add name="SPXmlContentMapProvider" siteMapFile="_app_bin/layouts.sitemap" type="Microsoft.SharePoint.Navigation.SPXmlContentMapProvider, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <add name="AdministrationQuickLaunchProvider" description="QuickLaunch navigation provider for the central administration site" type="Microsoft.Office.Server.Web.AdministrationQuickLaunchProvider, Microsoft.Office.Server.UI, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <add name="SharedServicesQuickLaunchProvider" description="QuickLaunch navigation provider for shared services administration sites" type="Microsoft.Office.Server.Web.SharedServicesQuickLaunchProvider, Microsoft.Office.Server.UI, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <add name="GlobalNavSiteMapProvider" description="CMS provider for Global navigation" type="Microsoft.SharePoint.Publishing.Navigation.PortalSiteMapProvider, Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" NavigationType="Global" EncodeOutput="true" />
        <add name="CombinedNavSiteMapProvider" description="CMS provider for Combined navigation" type="Microsoft.SharePoint.Publishing.Navigation.PortalSiteMapProvider, Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" NavigationType="Combined" EncodeOutput="true" />
        <add name="CurrentNavSiteMapProvider" description="CMS provider for Current navigation" type="Microsoft.SharePoint.Publishing.Navigation.PortalSiteMapProvider, Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" NavigationType="Current" EncodeOutput="true" />
        <add name="CurrentNavSiteMapProviderNoEncode" description="CMS provider for Current navigation, no encoding of output" type="Microsoft.SharePoint.Publishing.Navigation.PortalSiteMapProvider, Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" NavigationType="Current" EncodeOutput="false" />
        <add name="SiteDirectoryCategoryProvider" description="Site Directory category provider" type="Microsoft.SharePoint.Portal.WebControls.SiteDirectoryCategoryProvider, Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <add name="MySiteMapProvider" description="MySite provider that returns areas and based on the current user context" type="Microsoft.SharePoint.Portal.MySiteMapProvider, Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <add name="MySiteLeftNavProvider" description="MySite Left Nav provider that returns areas and based on the current user context" type="Microsoft.SharePoint.Portal.MySiteLeftNavProvider, Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <add name="UsagePagesSiteMapProvider" description="Provider for navigation in Portal Usage pages" type="Microsoft.SharePoint.Portal.Analytics.UsagePagesSiteMapProvider, Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
        <add name="PWASiteMapProvider" description="Project Web Access provider for global navigation" type="Microsoft.Office.Project.PWA.PWASiteMapProvider, Microsoft.Office.Project.Server.PWA, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" mode="Quick Launch" securityTrimmingEnabled="true" initializeMetadata="true" />
      </providers>
    </siteMap>

Good Luck!


More Information about SiteMapProvider

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.navigation.portalsitemapprovider_members.aspx

Advertisements

One Response to “How to retrieve Sites and Pages from the SiteMapProvider recursively”

  1. Bharat Sukhwal said

    What is the lastChild in the

    private void getRecursiveNavigation(SiteMapNode pageNode, XmlNode lastChild)

    Method.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: