How to: Extend the Navigation Provider in SharePoint Server 2010 (ECM)

Applies to: SharePoint Server 2010

You can extend the navigation provider in Microsoft SharePoint Server 2010 by deriving a custom provider class from any of the default site map providers. Deriving from a SharePoint Server 2010 site map provider supplies several benefits, such as navigation node caching and security trimming. The following code example derives from the PortalSiteMapProvider class and demonstrates how to add items to the site map provider. Although the site map provider classes have approximately twenty abstract or virtual methods, only a small number must be overridden and implemented in a custom site map provider.

How to extend the navigation provider

  1. Create a Microsoft Visual C# class library project with the following code, add the required references, and then build the project.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.SharePoint.Publishing;
    using Microsoft.SharePoint.Publishing.Navigation;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Configuration;
    
    namespace MyCustomNav
    {
    public class Navigation: PortalSiteMapProvider
    {
    public override SiteMapNodeCollection GetChildNodes(System.Web.SiteMapNode 
    node)
    {
    PortalSiteMapNode pNode = node as PortalSiteMapNode;
    if (pNode != null)
    {
    if (pNode.Type == NodeTypes.Area)
    {
    SiteMapNodeCollection nodeColl = base.GetChildNodes(pNode);
    SiteMapNode childNode = new SiteMapNode(this, 
    "<https://www.microsoft.com>", "<https://www.microsoft.com>", "Microsoft");
    
    SiteMapNode childNode1 = new SiteMapNode(this, 
    "<https://support.microsoft.com>", "<https://support.microsoft.com>", "Support");
    
    nodeColl.Add(childNode);
    
    SiteMapNodeCollection test = new SiteMapNodeCollection();
    test.Add(childNode1);
    childNode.ChildNodes = test;
    
    return nodeColl;
    }
    else
    return base.GetChildNodes(pNode);
    }
    else
    return new SiteMapNodeCollection();
    }
    }
    }
    
    Imports System
    Imports System.Collections.Generic
    Imports System.Text
    Imports Microsoft.SharePoint.Publishing
    Imports Microsoft.SharePoint.Publishing.Navigation
    Imports System.Web
    Imports System.Web.UI.WebControls
    Imports System.Configuration
    
    Namespace MyCustomNav
        Public Class Navigation
            Inherits PortalSiteMapProvider
            Public Overrides Function GetChildNodes(ByVal node As System.Web.SiteMapNode) As SiteMapNodeCollection
                Dim pNode As PortalSiteMapNode = TryCast(node, PortalSiteMapNode)
                If pNode IsNot Nothing Then
                    If pNode.Type = NodeTypes.Area Then
                        Dim nodeColl As SiteMapNodeCollection = MyBase.GetChildNodes(pNode)
                        Dim childNode As New SiteMapNode(Me, "<https://www.microsoft.com>", "<https://www.microsoft.com>", "Microsoft")
    
                        Dim childNode1 As New SiteMapNode(Me, "<https://support.microsoft.com>", "<https://support.microsoft.com>", "Support")
    
                        nodeColl.Add(childNode)
    
                        Dim test As New SiteMapNodeCollection()
                        test.Add(childNode1)
                        childNode.ChildNodes = test
    
                        Return nodeColl
                    Else
                        Return MyBase.GetChildNodes(pNode)
                    End If
                Else
                    Return New SiteMapNodeCollection()
                End If
            End Function
        End Class
    End Namespace
    
  2. Copy the .dll file that you created in step 1 into the SharePoint Server 2010 virtual directory’s bin folder.

  3. Create the following entry in the web.config file for the Web application, and then set the trust level to Full.

    <add name="MyCustomNavigationProvider" type="MyCustomNav.Navigation, MyCustomNav" 
    NavigationType="Current" />
    
  4. Create a custom master page, and then add the following code under the top navigation’s ContentPlaceHolder element.

    <SharePoint:AspMenu
    ID="TopNavigationMenu"
      Runat="server"
      DataSourceID="topSiteMap1"
      EnableViewState="false"
      AccessKey="<%$Resources:wss,navigation_accesskey%>"
      Orientation="Horizontal"
      StaticDisplayLevels="1"
      MaximumDynamicDisplayLevels="3"
      DynamicHorizontalOffset="0"
      StaticPopoutImageUrl="/_layouts/images/menudark.gif"
      StaticPopoutImageTextFormatString=""
      DynamicHoverStyle-BackColor="#CBE3F0"
      SkipLinkText=""
      StaticSubMenuIndent="0"
      CssClass="ms-topNavContainer">
    <StaticMenuStyle/>
    <StaticMenuItemStyle CssClass="ms-topnav" ItemSpacing="0px"/>
    <StaticSelectedStyle CssClass="ms-topnavselected" />
    <StaticHoverStyle CssClass="ms-topNavHover" />
    <DynamicMenuStyle BackColor="#F2F3F4" BorderColor="#A7B4CE" 
      BorderWidth="1px"/>
    <DynamicMenuItemStyle CssClass="ms-topNavFlyOuts"/>
    <DynamicHoverStyle CssClass="ms-topNavFlyOutsHover"/>
    <DynamicSelectedStyle CssClass="ms-topNavFlyOutsSelected"/>
    </SharePoint:AspMenu>
    <asp:SiteMapDataSource
      ShowStartingNode="False"
      SiteMapProvider="MyCustomNavigationProvider"
      id="topSiteMap1"
      runat="server"
      StartFromCurrentNode="true"/>
    
  5. Reset Microsoft Internet Information Server (IIS). Your SharePoint Server site should now show the updated navigation from the extended navigation provider.

See Also

Concepts

How to: Customize Navigation in SharePoint Server 2010 (ECM)

Working with Menus and Navigation Objects in SharePoint Server 2010 (ECM)