Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Shared Navigation for SharePoint Site Collections

5.00/5 (5 votes)
29 May 2009CPOL5 min read 97.6K   634  
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)