Click here to Skip to main content
15,861,168 members
Articles / Web Development / ASP.NET

Shared Navigation for SharePoint Site Collections

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
29 May 2009CPOL5 min read 95.6K   634   18   25
A SharePoint navigation provider for sharing top navigation with any site collection inside the same farm.

Introduction

By now you have already noticed there is no out-of-the-box way in SharePoint to have more than one site collection sharing the same top navigation. I hope that’s why you are here, because this is what this code is about.

Shared navigation for site collections is a very desirable feature in scenarios where, for example, you create a site collection for each department in your organization and place them under a parent site collection. Many organizations choose to create a site collection for each department, due to security concerns or to better manage the content databases. So, they want them to be isolated but at the same time share the same navigation.

This article is about a SharePoint navigation provider I wrote, that lets you borrow the top navigation from a specified site collection. This way you can have unlimited number of site collections sharing the same navigation.

First, I will show you how to implement and use the navigation provider, and later, I'll dive briefly into the code. Let's start with:

How Does it Work

SharedNavigationProvider is a navigation provider. It has to be registered in web.config, and has a parameter to indicate the source site collection for navigation. The provider will parse the navigation of the indicated source site collection and prepare it for the current site. The AspMenu control in the default master page will need to be modified to use the data source from SharedNavigationProvider.

How to Install and Use

There is no installation package or feature yet, but I do plan to create them when time permits. So, for now, installation instruction is a list of steps to modify the web.config and the master page.

There are two major requirements:

  1. All site collections must be on the same farm. They can be part of different applications, or part of the same application, but must be on the same farm.
  2. All webs implementing this navigator must have the Office SharePoint Server Publishing feature activated.

And, there are three major steps for installation:

  1. Add the DLL to the GAC
  2. Add the navigation provider to the web.config
  3. Modify the master page to use SharedNavigationProvider

1. Add the DLL to the GAC

  • I added a small .bat file, AddToGAC.bat, to the root of the project that does just that.
  • I am assuming you have gacutil.exe in c:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\. If not, you should find a way to get it. (For more info about gacutil, please see: http://msdn.microsoft.com/en-us/library/ex0ss12c(VS.85).aspx).
  • You must repeat this step on all front web servers.

2. Add the Navigation Provider to the web.config

  • Locate the web.config of your site collection (actually of your web application) and add the following provider entry:
  • XML
    <!-- ... -->
    <system.web>
        <siteMap >
            <providers>
            <!-- ... -->
            <add name="SharedNavigationProvider" 
               SourceSite="http://SourceSiteCollection" 
               NavigationType="Global" EncodeOutput="true" 
               type="DataQ.SharePoint.Providers.SharedNavigationProvider, 
                     SharedNavigationProvider, Version=1.0.0.0, 
                     Culture=neutral, PublicKeyToken=fc448b9e121af773" />
            </providers>
    <!-- ... -->
  • Set the SourceSite parameter to be the URL of the source site collection from where you want to borrow the navigation.
  • Repeat this step on all front web servers, if you have more than one.

3. Modify the Master Page to Use SharedNavigationProvider

Your master page needs to be modified to use the provider. You can do this in more than one way. You may use SharePoint Designer, or you may download the file, modify it, then upload, publish, and approve it. I don't think it is necessary to elaborate on that, but if you feel I should, drop me a message, and I'll do.

I will describe here what you need to change once you have the default.master (or whatever.master page you have set for your site) file opened.

  1. Be sure you keep a backup copy of the master page you are going to change.
  2. Be sure you are doing 1.
  3. Open the master page in your HTML editor of choice.
  4. Locate this control: <SharePoint:DelegateControl runat="server" ControlId="TopNavigationDataSource">, and underneath it, place this control:
  5. <asp:SiteMapDataSource 
        ShowStartingNode="False"
        SiteMapProvider="SharedNavigationProvider"
        id="topSiteMap2"
        runat="server"/>
  6. Locate this control: <SharePoint:AspMenu ID="TopNavigationMenu" ….. You should find it above the control we modified in 4.
  7. Change the attributes DataSourceID to topSiteMap2 and StaticDisplayLevels to 1:

    DataSourceID="topSiteMap2"
    StaticDisplayLevels="1"
  8. If you use the out-of-the-box default.master page, all these changes should look like this:
  9. <SharePoint:AspMenu
          ID="TopNavigationMenu"
          Runat="server"
          DataSourceID="topSiteMap2"
          EnableViewState="false"
          AccessKey="<%$Resources:wss,navigation_accesskey%>"
          Orientation="Horizontal"
          StaticDisplayLevels="1"
          MaximumDynamicDisplayLevels="1"
          DynamicHorizontalOffset="2"
          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>
            
    <SharePoint:DelegateControl runat="server" 
              ControlId="TopNavigationDataSource">
        <Template_Controls>
            <asp:SiteMapDataSource
              ShowStartingNode="False"
              SiteMapProvider="SPNavigationProvider"
              id="topSiteMap"
              runat="server"
              StartingNodeUrl="sid:1002"/>
        </Template_Controls>
    </SharePoint:DelegateControl>
    
    <asp:SiteMapDataSource 
      ShowStartingNode="False"
      SiteMapProvider="SharedNavigationProvider"
      id="topSiteMap2"
      runat="server"/>
  10. When you upload the modified master page, don’t forget there are three important steps to follow:
    1. Check in
    2. Publish as a Major Version
    3. Approve the new master page

The Code

I am not going to comment a lot here about the code inside this article, but I ensure you the code itself is well commented. So, if you plan to change it, you should find helpful hints about what I am doing.

In short, the idea of the navigation provider is to parse the source navigation structure and provide it as a dataset to the destination site collection.

Known Issues

  • The target attribute in the source navigation is not honored in the replicated navigations. This is something I plan to change in the next version.
  • There is an issue with navigation caching when you enable the source site collection to display subsites. When you add new subsites, it will not show right away in the shared navigation. To get around this, you should just move up and down the node in the source navigation and the caching is fixed.

What's Next

The code, of course, can be improved:

  • It can use smarter caching
  • It can add an admin page to the Site Sittings to set the source site collection
  • All installation steps can be automated and packaged into a solution package.

History

  • May 29th, 2009 - Published.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Data Quadrant, Inc.
United States United States
Seek, create and combine solutions in the realm of Information Portals, Business Inteligence and Business Automation. With a focus on SharePoint. (Houston, TX)

Comments and Discussions

 
QuestionI have one Top site nevigation on my site. I want another Site navigation on same site. Can you please provide code or success stories Pin
Nitin khankar5-May-16 0:18
Nitin khankar5-May-16 0:18 
QuestionSharePoint 2013 Pin
Member 1026781811-Sep-13 9:40
Member 1026781811-Sep-13 9:40 
AnswerRe: SharePoint 2013 Pin
Member 1026781823-Jan-14 7:47
Member 1026781823-Jan-14 7:47 
QuestionWorks for 2010 BUT Subsite drop-down does not extend all the levels Pin
Abhrajit_d3-Jul-12 10:30
Abhrajit_d3-Jul-12 10:30 
QuestionSites vs links in navigation Pin
Philip.izod31-Oct-11 2:26
Philip.izod31-Oct-11 2:26 
GeneralThank you. Navigation is working for 2010 site collections also.. [modified] Pin
AnuMattamudhre19-May-11 2:45
AnuMattamudhre19-May-11 2:45 
GeneralFeature was requested too... thank you [modified] Pin
Sandro Mueller26-Apr-11 5:09
Sandro Mueller26-Apr-11 5:09 
GeneralThere's a great solution that based on the same Idea Pin
bob.cedric15-Jun-10 8:03
bob.cedric15-Jun-10 8:03 
Generalsub menu in navigation not showing up Pin
myhiding24-May-10 10:31
myhiding24-May-10 10:31 
GeneralShared Navigation for SharePoint Site Collections is not working in my sitecollection Pin
goutam kumar Chak10-Mar-10 4:37
goutam kumar Chak10-Mar-10 4:37 
GeneralRe: Shared Navigation for SharePoint Site Collections is not working in my sitecollection Pin
Christopher TS16-Mar-10 18:00
Christopher TS16-Mar-10 18:00 
GeneralAudience Targeting Pin
Member 17229825-Mar-10 8:55
Member 17229825-Mar-10 8:55 
GeneralSecurity Trimming Pin
Member 28654849-Feb-10 0:38
Member 28654849-Feb-10 0:38 
GeneralRe: Security Trimming Pin
Marian Dumitrascu10-Feb-10 5:05
professionalMarian Dumitrascu10-Feb-10 5:05 
GeneralYOU ARE A GENIUS Pin
rutuma11-Dec-09 10:15
rutuma11-Dec-09 10:15 
QuestionCan this work across web applications Pin
jte36925-Sep-09 4:56
jte36925-Sep-09 4:56 
Generalcustomized header links Pin
mbruni.mka2-Sep-09 9:55
mbruni.mka2-Sep-09 9:55 
GeneralCannot get this working on MOSS 2007 sp1 Pin
shikarishambu7031-Jul-09 8:54
shikarishambu7031-Jul-09 8:54 
GeneralRe: Cannot get this working on MOSS 2007 sp1 Pin
Marian Dumitrascu31-Jul-09 9:29
professionalMarian Dumitrascu31-Jul-09 9:29 
GeneralRe: Cannot get this working on MOSS 2007 sp1 Pin
webwyse3-Sep-09 11:45
webwyse3-Sep-09 11:45 
GeneralRe: Cannot get this working on MOSS 2007 sp1 Pin
Dhruv198410-Dec-09 2:54
Dhruv198410-Dec-09 2:54 
GeneralWSS Pin
Jason L12-Jun-09 7:25
Jason L12-Jun-09 7:25 
GeneralRe: WSS Pin
Marian Dumitrascu12-Jun-09 7:29
professionalMarian Dumitrascu12-Jun-09 7:29 
GeneralRe: WSS Pin
jelliotboston16-Oct-09 12:11
jelliotboston16-Oct-09 12:11 
GeneralRe: WSS Pin
Marian Dumitrascu16-Oct-09 13:32
professionalMarian Dumitrascu16-Oct-09 13:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.