在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();