A Single Breadcrumb for All SharePoint Pages

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值