I had a requirement to ensure that all sites within the site collection would display the full site structure (up to the 1st level sub-site) on the left navigation. Given the site structure below for example:

Home
Site 1
Site 1.1
Site 1.1.1
Site 1.2
Site 1.2.1
Site 2
Site 2.1
Site 2.1.1
Site 2.2

For any site underneath Site 1, the following should be shown on the left nav:

Site 1
Site 1.1
Site 1.1.1
Site 1.2
Site 1.2.1

For any site underneath Site 2, the following should be shown on the left nav:

Site 2
Site 2.1
Site 2.1.1
Site 2.2

This can be achieved by tweaking the StartingNodeOffset property of the PortalSiteMapDataSource. The PortalSiteMapDataSource is a SharePoint control that typically sits on the masterpage and provides data for the menu controls on the page. See http://blogs.msdn.com/b/ecm/archive/2007/02/10/moss-navigation-deep-dive-part-1.aspx for more information about this control and other important elements related to navigation in SharePoint.

When the StartFromCurrentNode = true and StartingNodeOffset = -1, the first node in the left nav will be the parent of the current site. If StartingNodeOffset = -2, then the first node is the parent of the parent of the current site, and so on.

The problem however is OOTB the PortalSiteMapDataSource is defined in the masterpage and hence the StartingNodeOffset is static and the same for all sites. If you set it to -2, it will work for site 2.1.1 but will not work for site 2.1 or 2.1.1.1 for example.

To address this you can developed a custom PortalSiteMapDataSource which will inherit the OOTB class. It is actually relatively simple.

Step 1: Create the custom PortalSiteMapDataSource

In this class we override the GetHierarchicalView method and calculate the ‘level’ of the current site and adjust the StartingNodeOffset property accordingly.

Step 2: Update the master page to use the custom PortalSiteMapDataSource

Remember to never modify the OOTB master pages. Create a copy of the master page (e.g. v4.master, or nightandday.master) and add the following to the top of the page to register our custom PortalSiteMapDataSource as a user control:

The key here is changing from the OOTB class to our class. We also need to set ShowStartingNode to true.

Step 3: Register custom PortalSiteMapDataSource as safe control

This is the final step. If you have built the custom PortalSiteMapDataSource in its own project and reference it from your main project, then you can use Visual Studio’s Package Explorer to add the project to the package and add our custom PortalSiteMapDataSource as a safe control.

If you have everything in one single project, then you will have to manually edit the manifest template of the package to include the PortalSiteMapDataSource as a safe control as Visual Studio does not seem to pick it up automatically.

That’s it – go ahead and deploy your solution.

Advertisements

Share this:

Like this:

LikeLoading...

Related

About Bernado

Based in Australia, I am a freelance SharePoint and Dynamics CRM developer. I love developing innovative solutions that address business and everyday problems. Feel free to contact me if you think I can help you with your SharePoint or CRM implementation.