Thursday 27 September 2012

Cross web application navigation SharePoint 2010- Part I

 Cross web application navigation SharePoint 2010- Part I

 In real life Sharepoint applications it is common practice to have consistent global navigation (or top navigation bar) across site. It is achievable out of the box with Sharepoint navigation architecture. But it is also common situation when site collection (SPSite) contains multiple sites (SPWeb) and there is a requirement to have consistent navigation when users goes through sites within site collection (it can be navigation items from one of the particular sites, e.g. site collection root site, or it can be navigation items retrieved from custom storage like xml file, database, etc). More complicated case – is when navigation should be preserved within multiple site collections.

This is problem scope which I would like to address in the several posts including this one. I will go from simple to complex and start from most simple scenario: when we have single site collection and several sites under it. First case will assume that we are limited by WSS only. It means neither site collection root web nor its subsites are publishing sites. Publishing sites (sites with publishing infrastructure) have their own navigation API (see previous post) – and it is much more complicated task to preserve cross-publishing sites navigation. I will describe it in next part of navigation-related posts.

Sites which were created using one of WSS template initially use one of the following navigation data providers:
  • SPNavigationProvider - provides a base class for Windows SharePoint Services site-map providers that are specialized for SharePoint site navigation;
  • SPSiteMapProvider - provides the SiteMapNode objects that constitute the global content breadcrumb, which represents objects in the site hierarchy above the current site;
  • SPContentMapProvider - provides methods and properties for implementing a site map provider for contents of a Windows SharePoint Services site. This class provides the SiteMapNode objects that constitute the content breadcrumb, where “content” referes to the lists, folders, items, and list forms composing the breadcrumb;
  • SPXmlContentMapProvider - provides methods and properties for implementing a site map provider for contents of a Windows SharePoint Services site. This class provides the SiteMapNode objects that constitute the content breadcrumb, where “content” referes to the lists, folders, items, and list forms composing the breadcrumb.
In order to cover 2 mentioned cases (navigation is preserved from one of the subsites and navigation is preserved globally) we need consider 2 of these navigation providers: SPNavigationProvider  and SPXmlContentMapProvider.
SPNavigationProvider  will help us if need to keep navigation from one particular site. If we will look the code of SPNavigationProvider using reflector we will found that it has public virtual Web property which returns current SPWeb instance:

    public class SPNavigationProvider : SiteMapProvider
    {
        ...
        protected virtual SPWeb Web
       {
            get
            {
                return SPControl.GetContextWeb(HttpContext.Current);
          }
       }
   }
 
But what is more important is that SPNavigationProvider uses this property as a source for navigation data, i.e. it will return collection of SiteMapNode instances for this web site. So in order to make navigation consistent within all sites we need to implement custom navigation provider by inheriting SPNavigationProvider and override its Web property

    public class SingleWebNavigationProvider : SPNavigationProvider
   {
        ...
       protected override SPWeb Web
        {
           get
           {
                return SPContext.Current.Site.RootWeb;
           }
       }
   }
 
Here for example purposes site collection root site is used as a source of navigation data, i.e. custom navigation provider SingleWebNavigationProvider will always return navigation items from this SPWeb. In order to use this custom navigation provider in site we need to register it in web.config file of web application:

    <siteMap defaultProvider="SPNavigationProvider" enabled="true">
     <providers>
        ...
       <add name="SingleWebNavigationProvider"
    type="NamespaceName.SingleWebNavigationProvider, AssemblyName" />
      </providers>
   </siteMap>
 
And then modify master page:

    <asp:SiteMapDataSource
      ShowStartingNode="False"
      SiteMapProvider="SingleWebNavigationProvider"
      id="topSiteMap"
      runat="server"/>
 
After that whenever site you go – navigation data will be used from site collection root web. If you want to preserve navigation across site collections – you need to override Web property so it should return the same site regardless of current site collection (e.g. you can add URL of source web site using custom property of SingleWebNavigationProvider in web.config file and override Initialize(…) method) and modify master pages of all site collections in order to use SingleWebNavigationProvider in them.
Second case – preserving navigation globally can be achieved by using SPXmlContentMapProvider. Specify web application-relative path in its siteMapFile property in web.config:

    <siteMap defaultProvider="SPNavigationProvider" enabled="true">
      <providers>
        ...
        <add name="SPXmlContentMapProvider" siteMapFile="_app_bin/layouts.sitemap"
    type="Microsoft.SharePoint.Navigation.SPXmlContentMapProvider, Microsoft.SharePoint,
    Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
      </providers>
   </siteMap>
 
and modify master page:

    <asp:SiteMapDataSource
     ShowStartingNode="true"
      SiteMapProvider="SPXmlContentMapProvider"
      id="topSiteMap"
      runat="server"/>
 
After that you will have the same static navigation across all sites. If you want to preserve this navigation across site collections you will also need to modify their master pages.
This is all what I wanted to tell about in part 1. In next part II will show how to preserve cross-site and cross-site collection navigation for publishing sites.

No comments:

Post a Comment