In one of my previous posts I wrote about basic information about navigation architecture in Sharepoint. With this article I continue series of posts about navigation in Sharepoint. Here I will use information described in the previous post, so I recommend to read it before this one.
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 (Update 2010-12-19: see Cross-site and cross-site collection navigation in Sharepoint - part 2: publishing sites).
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:
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
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:
And then modify master page:
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:
and modify master page:
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 I will show how to preserve cross-site and cross-site collection navigation for publishing sites. Update 2010-12-19: see Cross-site and cross-site collection navigation in Sharepoint - part 2: publishing sites.