Microsoft  CSIP   WindowsMobile   MEDC   合作伙伴   联合实验室 设为首页 加到收藏夹

 
登录       点击换一张     注册新用户 找回密码

[SharePoint]如何在Custom Action定义中包含当前页的URL
2008年11月18日03:22   编辑:黎波 来源: 博客园 浏览: 1016

关于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

 

发表评论
评论标题 :
评论内容 :

    查看评论

请您注意:遵守国家有关法律、法规,尊重网上道德,承担一切因您的行为而直接或间接引起的法律责任。 本站拥有管理笔名和留言的一切权利。
相关文章推荐
· ASP.NET 2.0高级数据处理之处理Null值 2008-5-13  陶刚编译
· ASP.NET 2.0高级数据处理之主从数据表 2008-5-13  陶刚编译
· ASP.NET 2.0高级数据处理之使用参数 2008-5-13  陶刚编译
· 细节决定成败 ASP.NET中的蝴蝶效应 2008-5-13  马甲走江湖
· ASP.NET 2.0中层次数据的处理 2008-5-13  陶刚编译

  

论坛推荐  
热点活动
更多»

© CSIP 信息产业部软件与集成电路促进中心 All Rights Reserved 版权所有 京ICP备06020771号
联系电话:010-63951881-8003 王先生        邮件:wangj@csip.org.cn