Monday, January 9, 2012

Consistent Top Navigation Menu across All site collections in SharePoint 2010

Requirement: Display a consistent Top Navigation Menu for across site collections in SharePoint.

We can fulfill this requirement by using custom sitemap providers in SharePoint. Here are the steps at high level
  1. Create your own sitemap file in Layouts folder
  2. Add a Site Map Provider in web.config file
  3. Add a SiteMapDataSource to your master page
  4. change the SharePoint menu's datasource to your custom SiteMapDataSource
Lets start building SharePoint 2010 common navigation across site collections.

1. Create your own sitemap file in Layouts folder

Modify the site map to match your navigation needs and save it with a .sitemap extension in a location accessible by your SharePoint farm. The _LAYOUTS directory is a great place since any site can get to that location (i.e. _/layouts/BPCorp.sitemap)

Create the sitemap as per the following template code

<siteMap>
   <siteMapNode title="Container" url="" >
   <siteMapNode title="Global Portal" description="" url="/" />
           <siteMapNode title="Finance" description="" url="~/finance" >
          <siteMapNode title="EMEA" url="~/finance/EMEA" description="" />
            <siteMapNode title="APAC" url="~/finance/APAC" description="" />
            <siteMapNode title="CORP" url="~/finance/CORP" description="" />
         </siteMapNode>
 
        <siteMapNode title="Team" description="" url="~/team" />
        <siteMapNode title="Information Technology" description="" url="~/it" />
        <siteMapNode title="Legal" description="" url="~/legal" />
        <siteMapNode title="Regions" description="" url="" >

            <siteMapNode title="North America" url="~/regions/NA" description=""/>
            <siteMapNode title="Middle East" url="~/regions/me" description=""/>
       </siteMapNode>
   </siteMapNode>
</siteMap>

Site Map control crashes with error "An unexpected error has occurred." when you point to a page in the URL (instead of Site) and that site is with broken permissions. Fix is simple: Instead of : <siteMapNode title="EMEA" url="~/finance/EMEA/default.aspx">, use: ="~/finance/EMEA">. It also crashes when you have duplicate URLs in the siteMapNode!
2. Add a Site Map Provider in web.config file
Now that you have a site map you will need to add a provider in the web.config of the application that is going to use it. The providers section starts with <providers>. You will need to add a line similar to this.

<add name="BPCorpProvider" siteMapFile="_layouts/BPCorp.sitemap"
   type="System.Web.XmlSiteMapProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"  />

3. Add a SiteMapDataSource to your master page
Next the master page needs to be changed. Edit the Master page you will be using with this site map in SharePoint Designer. You will need to add an asp:SiteMapDataSource right above the SharePoint:AspMenu like the one shown below.

<asp:SiteMapDataSource ShowStartingNode="False" SiteMapProvider="BPCorpProvider" id="BPCorpSiteMap" runat="server"/>

5. Next you will need to change DataSourceID field in the ASPMenu to match the ID of the SiteMapDataSource as shown below. Make sure you set value for MaximumDynamicDisplayLevels="3" , otherwise you won't get sub-menus!

<SharePoint:AspMenu
      ID="TopNavigationMenuV4"
      Runat="server"
      EnableViewState="false"
      DataSourceID="BPCorpSiteMap"
      AccessKey="<%$Resources:wss,navigation_accesskey%>"
      UseSimpleRendering="true"
      UseSeparateCss="false"
      Orientation="Horizontal"
      StaticDisplayLevels="1"
      MaximumDynamicDisplayLevels="3"  
      SkipLinkText=""
      CssClass="s4-tn" />

Your code will look like this:
sharepoint 2010 consistent navigation across site collections
Save, check in, publish a major version, and approve your master page.

Please note: XML is case sensitive, and you got to replace any special codes like %20 (space) with the literal (i.e. a space). Also there are some special characters for XML like &amp; for &, &lt; for <, and &gt; for >

Finally, Here is what you will get:
Need Security trimming in SharePoint 2010 Top Navigation? Here is my another post: Security Trimmed Top Navigation Menu Using Custom Sitemap Provider for SharePoint



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

37 comments :

  1. How can you display your custom master page in application pages; settings.aspx, or viewlsts.aspx, etc.? Do you see multi-level in application pages?

    Thanks

    ReplyDelete
    Replies
    1. You have to edit the application.master file with these changes. Additionally to attach the custom application master, you can use http handlers.

      Delete
  2. Thank you for the insight. Do you happen to know if this works for Publishing site collections?

    ReplyDelete
  3. Yes, This will work for publishing sites as well.

    ReplyDelete
  4. The DataSourceID of 'TopNavigationMenuV4' must be the ID of a control of type IHierarchicalDataSource. A control with ID 'XXXCustomProvider' could not be found.

    i got hte above error

    ReplyDelete
  5. hey sorry i given wrong id, now its working perfect. thanks man. u saved my time a lot.

    ReplyDelete
  6. All of these links are static though, I assume when more pages or sub-sites are added you'll need to manually modify that sitemap file to include those new links. Is there a way to automate that or have it dynamically updated? Also this doesn't take any security restrictions into account, or can it?

    ReplyDelete
    Replies
    1. True, You have to update the .Sitemap file in all your WFEs, Thats the limitation. Well, to automate this you can create a application page/webpart to update the sitemap file.

      As I mentioned in the Tail You can establish security trimming by following my another post: http://salaudeen.blogspot.com/2012/03/security-trimmed-sitemap-provider-for.html

      Delete
  7. Love your work, thanks for making the effort. Copied everything exactly as you wrote.

    I get Error parsing control - The siteMapProvider 'BPCorpProvider' cannot be found. I have the BPCorpProvider in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\web.config.

    Any ideas how to troubleshoot?

    ReplyDelete
    Replies
    1. You have to update the web.config file in your virtual directory! Go to IIS Manager and open your website's physical location, update the web.config file in that location!

      Delete
    2. Ah... I see how it works now. Brilliant. Just what I needed for my project. Thanks mate.

      Delete
  8. This is exactly what I was looking for. Thanks a Bunch!

    ReplyDelete
  9. how can we have highlighted tabs, because when we navigate from one sitecollection to another the tabs are not highlighted.How do we achieve this

    ReplyDelete
  10. how can we make the tabs highlighted when we navigate to other sitecollection, ineed to the tabs to be highlighted based on the location i am

    ReplyDelete
    Replies
    1. Try this solution: http://www.tcscblog.com/2011/11/21/sharepoint-2010-xml-navigation-with-selected-highlighted-tabs/ (I Haven't try but!)

      Delete
  11. thanks , but unfortunately it doesn't work , same issue.

    ReplyDelete
  12. Hi i need to create a online test for students in Sharepoint 2010 and also store the results in database. I think i should do it by Visual Webpart, but don't know how please post some material if you can it would be really helpful...Thanks in Advance !!

    ReplyDelete
  13. Salaudeen, thanks for putting this together. This solution does work as expected. However, if you enable the publishing features, it breaks. An error comes up, "The DataSourceID of 'TopNavigationMenuV4' must be the ID of a control of type IHierarchicalDataSource. A control with ID 'XXXCustomProvider' could not be found." If I deactivate the publishing features, it works again. I suppose I could just deactivate the navigation element of the publishing feature.

    Also, is the only way to view this navigation when on viewlists.aspx, settings.aspx to modify the application.master? If we had to modify this master page, it would affect all web applications on the WFE correct? Any chance you could shed more light on this solution with that respect?

    Thanks again for everything.

    ReplyDelete
    Replies
    1. Hi Pete,

      For Application pages, You can either brand the Application.master in Layouts folder or you can attach a HTTP module.

      Please ensure you are packing the changes as a Solution (wsp) and deploying it. Otherwise, it will not be replicated in other WFEs automatically. The error message "A control with ID 'XXXCustomProvider' could not be found" occurs when you miss the web.config changes or sitemap datasource id is different in master page.

      Regards,
      Sal

      Delete
  14. Hi Salaudeen

    I tried your solution but it does not work for me. May be I am missing something.

    I placed the MyCustomSiteMap.sitemap file in the
    "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS" folder of my APP server

    I changed the Web Config of my website ( by browsing to physical location of the config file i.e C:\inetpub\wwwroot\wss\VirtualDirectories\32995)

    When i edit the master page to add () and change the datasourceid, it comes up with the error
    Error parsing control - The siteMapProvider 'BPCorpProvider' cannot be found.

    Can you help please !

    Thanks
    Mandeep

    ReplyDelete
    Replies
    1. Mandeep,

      SiteMap File must be placed in WFE(s) and referenced in web.config files of WFE(s). Apparently the error message "The siteMapProvider 'BPCorpProvider' cannot be found." tells you are missing the above steps!

      Regards,
      Sal

      Delete
  15. Thank you so much for your article.

    The global navigation works fine when I first load the site but after clicking on any link in the nav bar all the rest of the links then point to a blank page. The a href for each link gets converted to a blank value (a href ="").

    Do you have any suggestions please?

    ReplyDelete
    Replies
    1. Strange! May be because of caching issues?

      BTW, Is your links pointing to Same Web Application?

      Delete
  16. I am getting the same error of sitemapprovider cannot be found. I have added the entry in both web.config files on my two WFEs. I have checked the provider ID and made sure it is correctly referenced in the master page. I have also reset IIS. Something is missing.

    ReplyDelete
  17. Hello, I followed the steps, and it 'almost' works correctly. The sub-sitemap nodes are showing up as top level menu items instead of submenu items, so 'NA', for example is showing as a top menu item, just like 'Regions'. Any idea why that might be? I checked the xml tags in the sitemap file and they are correct

    ReplyDelete
    Replies
    1. Which means: You have to wrap the sub-site node Inside top level node!!!

      Delete
  18. I also have the same problem as mentioned by anonymous. The sub menus are showing up right besides the top level menu item. I verfied that the sub menu items are correctly placed inside top level node.

    ReplyDelete
  19. Can i have a link the .aspx pages instead of sub-sites for the submenus?

    ReplyDelete
    Replies
    1. Sure, will work as long as the subsite inherits permissions from top level site!

      Delete
  20. Hello Salaudeen, I have followed the steps mentioned above. The sub menus are appearing next to the top level menu item instead of drop downs. I have checked the .sitemap file and it is correct. Any suggestions?

    ReplyDelete
    Replies
    1. Which means you have to change MaximumDynamicDisplayLevels as in point#5!

      Delete
  21. Since we do not want to have sub-sites, can i have site pages as a url instead of sub-site for sub-menus?

    ReplyDelete
    Replies
    1. Yes, you can link pages instead of sub-sites

      Delete
  22. will this method work with SharePoint 2013 for consistent global navigation across different site collection?

    ReplyDelete
  23. SharePoint 2013 has added Navigation management to its Managed Metadata Term Store. Great news, but it has one limitation: you cannot assign the same navigation across site collections. In other words, a given global navigation node can only be used once. Ridiculous. At least that's my experience.

    ReplyDelete
  24. Can this same functionality be used on SharePoint Foundation 2013? I have a requirement to provide a top-tier navigation component to span across several site collections. I have used a very similar approach with WSS v3 some years back and that spanned 55 site collections without any problems.

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...