为Sharepoint应用程序页添加面包屑导航

From:http://weblogs.asp.net/jan/archive/2008/04/16/adding-breadcrumb-navigation-to-sharepoint-application-pages-the-easy-way.aspx

 

UPDATE: 如果你需要为管理中心的页面添加面包屑导航,请参考这里:http://weblogs.asp.net/jan/archive/2008/10/10/adding-breadcrumb-navigation-to-application-pages-in-sharepoint-central-administration.aspx

不久前我写了一篇关于如何为自定义的应用程序页添加面包屑导航(就像Sharepoint自带的页面那样)的文章。技巧就在于为站点的_app_bin 文件夹下的layouts.sitemap 添加siteMapNode 节点。这种方式手动操作一下是很EASY,你要做的仅仅是用记事本或者VS打开layouts.sitemap 文件,在目标节点(比如Site Settings)下面增加一个siteMapNode 就完事了。 不过在生产环境中,特别是部署了多个前端服务器的场,你大概没心情这样去一个个手动改了吧!当然,我们可以通过一个Feature 处理事件,使用XMLDocument加载layouts.sitemap 并添加siteMapNode 后保存,这听起来似乎很简单,不过遇到多个前端服务器的场的时候麻烦又来了,你需要在所有的服务器上执行一次你的代码。为了解决这个问题你又得创建一个能在所有服务器上执行的定时任务,这里有个例子:over here.

上面描述的场景都还是有点麻烦,不过我的一位博客读者发现 Sharepoint其实已经内置了一个更为简单的方法可以实现我们想要的功能。首先你需要创建一个类似于layouts.sitemap.*.xml (*代表自定义的名称)的文件,比如layouts.sitemap.demoapppages.xml。这个文件需要放在C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS文件夹下。在这个文件里你可以加入本应该追加到layouts.sitemap 文件的siteMapNode 节点,比如:

<?xml version="1.0" encoding="utf-8"?>
<siteMap>
    <siteMapNode url="/_layouts/demopage.aspx"
    parentUrl="/_layouts/settings.aspx" title="Demo App Page"/>
</siteMap>

这里的parentUrl 属性代表siteMapNode 节点将被自动追加到layouts.sitemap 的哪个节点下(例子中的是Site Settings 页面),当创建一个新的Web Application时,Sharepoint会将layouts.sitemap.demoapppages.xml 的内容与layouts.sitemap  自动合并(这样layouts.sitemap 就被创建在_app_bin 目录下了)。为了强制对已经存在的 Web Application重新生成layouts.sitemap ,你可以使用STSADM的 copyappbincontent 操作:

STSADM -o copyappbincontent

或者你可以调用SPWebService 类的 ApplyApplicationContentToLocalServer 方法(通常在 Feature Receiver中调用):

public class FeatureHandler : SPFeatureReceiver
{
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
SPFarm.Local.Services.GetValue<SPWebService>().
                    ApplyApplicationContentToLocalServer();
    }

    public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {}

    public override void FeatureInstalled(SPFeatureReceiverProperties properties) {}

    public override void FeatureUninstalling(SPFeatureReceiverProperties properties) {}
}

我已经在这里创建了一个小工程来展现上面描述的方法, 包含下面的资源:

  • Custom Application Page (demopage.aspx), 部署在_layouts 目录下
  • Custom SiteMap (layouts.sitemap.demoapppages.xml)
  • Feature, web application 级别部署, 在Site Settings 页面为自定义应用程序页添加一个链接
  • Feature event handler 负责触发合并所有sitemap文件的事件

特此感谢 Brian Staton for sharing your tip!

 

译者补充:对于带ItemID和List参数的layouts页面,请确保Feature中Custom Action的链接参数为 " ?ID={ItemId}&amp;List={ListId}”,否则导航条不正确,且页面继承的MasterPage必须为 MasterPageFile="~/_layouts/application.master"

posted @ 2009-01-31 20:14  laputa'sky  阅读(617)  评论(0编辑  收藏  举报