A Single Breadcrumb for All SharePoint Pages

February 8 2010 One comment

SharePoint’s out-of-the-box breadcrumb control is kinda a strange animal. Basically you have three different kinds of breadcrumbs in SharePoint. Each of these breadcrumbs are using different provider for their back-end:

  • Layout Pages / Administration Pages use SPXmlContentMapProvider,
  • Publishing Pages use CurrentNavSiteMapProviderNoEncode, and
  • Form Pages (AllItems.aspx, EditForm.aspx, DisplayForm.aspx, etc.) use SPContentMapProvider

This is all fine, when you have different master pages, or when you override the master page’s breadcrumb place holder with appropriate breadcrumb control defined in your page layout. In out-of-the-box publishing site templates the breadcrumb handling goes something like this:

  • Application.master defines a breadcrumb for layouts pages
  • Publishing page layouts override master page’s (e.g. default.master) breadcrumb (either by overriding PlaceHolderTitleBreadcrumb, or hiding PlaceHolderTitleBreadcrumb and pushing a new one in PlaceHolderMain, like in DefaultLayout.aspx)
  • default.master defines a default breadcrumb that is used for everything else, like for example form pages, and non-publishing team site page layouts

All this creates a cruft in you page layouts, because you have to repeat the same overrides in all your page layouts. This is also a problem, when you want to have a single master page for all your pages (using an HttpModule to override application.master and setting both custom master and system master to point to the same master page).

This isn’t a problem if you replace the default breadcrumb with a more intelligent one. Here is a code for a generic breadcrumb control that works for all kinds of pages described above:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Publishing.Navigation;
using Microsoft.SharePoint.WebControls;

namespace Sininen.Meteoriitti.SharePoint.Web.UI
{
    public class Breadcrumb : UserControl
    {
        protected SiteMapPath ContentMap;
       
        public Breadcrumb()
        {
            Load += BreadcrumbLoad;
        }

        void BreadcrumbLoad(object sender, EventArgs e)
        {
            if (Page is UnsecuredLayoutsPageBase)
            {
                ContentMap.SiteMapProvider = "SPXmlContentMapProvider";
            }
            else if (Page is PublishingLayoutPage)
            {
                ContentMap.RenderCurrentNodeAsLink = false;
               
                var provider =
                    SiteMap.Providers["CurrentNavSiteMapProviderNoEncode"] as
                    PortalSiteMapProvider;

                if (provider != null)
                {
                    provider.IncludePages =
                        PortalSiteMapProvider.IncludeOption.Always;
                    ContentMap.Provider = provider;
                }
                else
                {
                    ContentMap.SiteMapProvider =
                        "CurrentNavSiteMapProviderNoEncode";
                }
            }
            else
            {
                ContentMap.SiteMapProvider = "SPContentMapProvider";
            }
        }
    }
}

And the ascx-file for the above code behind:

<div class="breadcrumb">
    <asp:SiteMapPath
        id="ContentMap"
        SkipLinkText=""
        NodeStyle-CssClass="ms-sitemapdirectional"
        runat="server" />
</div>

Now you just have to replace the PlaceHolderTitleBreadcrumb place holder’s content with the new breadcrumb user control in your master page.

Bookmark and Share

Popularity: 72% [?]

One comment to “A Single Breadcrumb for All SharePoint Pages”

  1. [...] check it out! These guys already have a batch of posts, ranging from topics like breadcrumb navigation all the way to an installation pitfall checklist. And more is coming… So if you’re into [...]

Leave a Reply