在SharePoint中动态添加修改Custom Action

本文介绍了如何在SharePoint中通过代码动态地添加和修改Custom Action,以实现更灵活的功能调整。传统方法依赖配置文件,修改后需重启IIS。通过SPUserCustomAction对象和SPSite/SPWeb的UserCustomActions属性,可以实现动态操作,包括添加、更新Custom Action的title、description和URL等属性。
摘要由CSDN通过智能技术生成

在SharePoint中,常用的添加一个custom action的方法是使用配置文件,例如下面的配置文件向SharePoint中添加了一个custom action:

<CustomAction
    Id="SharePoint.TestLab.CustomAction"
    GroupId="PersonalActions"
    Location="Microsoft.SharePoint.StandardMenu"
    Sequence="1000"
    Description="SharePoint.TestLab.CustomAction Description"
    Title="SharePoint.TestLab.CustomAction Title"
    <UrlAction Url="~site/_layouts/SharePoint.TestLab.CustomAction/CustomAction.aspx"/>
  </CustomAction>

从配置信息中可以知道,这个custom action是在切换用户的下拉菜单中添加了一个link,指向一个页面。

这样的custom action一旦部署就不能动态的修改了,例如修改title和description,以及URL,location等都需要改配置文件,重启IIS才能生效。

如果想根据用户的设置来动态的添加和修改custom action,需要使用代码来完成。

SharePoint提供了“SPUserCustomAction”这个对象(仅支持SharePoint 2010和2013),以及在SPSite和SPWeb对象上的“UserCustomActions”属性来完成这个功能,以下是部分示例代码:

向Web中(也可以在Site中,使用SPSite.UserCustomActions属性,这取决于custom action的使用范围)添加一个custom action:

    SPUserCustomAction action = Web.UserCustomActions.Add();
    action.Title = "SharePoint.TestLab.CustomAction Title";
    action.Description = "SharePoint.TestLab.CustomAction Description";
    action.Group = "PersonalActions";
    action.Location = "Microsoft.SharePoint.StandardMenu";
    action.Sequence = 1000;
    action.Url = "~site/_layouts/SharePoint.TestLab.CustomAction/CustomActionSiteSetting.aspx";
    action.Name = "SharePoint.TestLab.CustomAction";
    action.Update();
从UserCustomActions集合中获取一个custom action:

    var action = Web.UserCustomActions.FirstOrDefault(
                        item =>
                            item.Name == "SharePoint.TestLab.CustomAction");

我试着使用action.Delete()方法来删除一个action,然后update action,update web,但是失败了,似乎这个删除操作是有问题的,具体的原因还不清楚,这个delete方法本质上是调用了一个存储过程(proc_DeleteCustomAction),从数据库中删除一个custom action。

如果不能删除,可以改变action的location属性,来隐藏它,也可以达到一样的目的:

     action.Location = "Removed";
     action.Update();



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值