[SharePoint]如何在Custom Action定义中包含当前页的URL

关于Custom Action的介绍,我就不再重复了,因为SDK已经非常详细。这里提供一些资料供大家参考:
什么是Custom Action?
如何定义一个Custom Action?
Custom Action定义的参考

简单的说,通过定义Custom Action可以在SharePoint的界面上添加菜单项。

本文要解决的问题是:如何在Custom Action定义中包含当前页的URL?

Custom Action提供了一些URL Token,使得Custom Action的URL能够根据当前上下文设置不同的参数:
~site - Web site (SPWeb) relative link.
~sitecollection - site collection (SPSite) relative link.
{ItemId} - Integer ID that represents the item within a list.
{ItemUrl} - URL of the item being acted upon. Only work for documents in libraries. [Not functional in Beta 2]
{ListId} - GUID that represents the list.
{SiteUrl} - URL of the Web site (SPWeb).
{RecurrenceId} - Recurrence index. This token is not supported for use in the context menus of list items.

默认提供的Token很常用,但还不够用。我们经常需要在Custom Action的URL中带上Source参数,参数的值为被点击的Custom Action所在的页面的URL。例如下面的URL:

http://wss.u2ucourse.com/Lists/Links/NewForm.aspx?Source=http%3A%2F%2Fwss%2Eu2ucourse%2Ecom%2Fdefault%2Easpx

这样我们就可以从Custom Action指向的页面返回到Custom Action所在的页面。

举个例子,我们在文档库列表的“设置”菜单组增加了一个叫“设置工作流节点”的Custom Action,点击以后会导向一个工作流节点设置的Application Page页面。我们希望在这个页面处理完成之后,能够返回到点击了“设置工作流节点”的页面,以便用户能够继续执行其他操作。如果能够在导向Application Page的URL中带上Source参数,就能够很方便的从Application Page返回到原始页面,而不是都返回到文档库根目录的默认视图页面。因此,我们很需要一个名字可能叫{CurrentUrl}的URL Token,可是目前没有提供。

现在已经把问题描述清楚了,接下来要介绍一种解决方法,也是从别人那学来的。

在Custom Action的定义中,UrlAction元素是支持javascript的,所以我们可以玩玩小技巧,借助javascript的字符串拼接和window.location来达到我们的目的。

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    
<CustomAction
        
Id="{6FCB0F81-2105-4d9f-96BF-C48A19B8E439}"
        Title
="My Link"
        Location
="Microsoft.SharePoint.StandardMenu"
        GroupId
="SettingsMenu">
        
<UrlAction Url="javascript:window.location= '{SiteUrl}/_layouts/mypage.aspx?List={ListId}&amp;Source=' + window.location"/>
    
</CustomAction>
</Elements>

如果你的Url中同一个Token出现了两次,那就要变通一下了,因为SharePoint只会对第一个Token进行替换(这不知道算不算是bug)。可以用下面的方法:

<UrlAction Url="javascript:function process(){var site='{SiteUrl}';var item={ItemId};window.location.href=site+'/Lists/MyList/NewForm.aspx?ID='+item+'&amp;Source='+site+'/Lists/myOtherList/DispForm.aspx?ID='+item;};process();"/>


当初我看到这个方法时,不禁感叹JavaScript真是Web开发的救命药啊!

参考:Using the Current Page URL in the UrlAction of a SharePoint Feature

作者:黎波
博客:http://bobli.cnblogs.com/
日期:2008年10月20日

posted @ 2008-10-20 23:22  黎波  阅读(1334)  评论(1编辑  收藏  举报