如何在SharePoint中以编程方式添加自定义菜单操作?
我需要在c#中以编程方式向自定义内容类型添加自定义菜单操作。这是因为我不知道我需要事先链接到的URL。激活该功能时,将从配置中提取要链接到的URL。 我尝试了以下方法: 在my Element.xml文件中添加了CustomAction,如下所示:如何在SharePoint中以编程方式添加自定义菜单操作?,sharepoint,content-type,custom-action,Sharepoint,Content Type,Custom Action,我需要在c#中以编程方式向自定义内容类型添加自定义菜单操作。这是因为我不知道我需要事先链接到的URL。激活该功能时,将从配置中提取要链接到的URL。 我尝试了以下方法: 在my Element.xml文件中添加了CustomAction,如下所示: <CustomAction Id="MyID" RegistrationType="ContentType" RegistrationId="0x010100ef19b15f43e64355b3943139
<CustomAction
Id="MyID"
RegistrationType="ContentType"
RegistrationId="0x010100ef19b15f43e64355b39431399657766e"
Location="EditControlBlock"
Sequence="1000"
Title="My Menu Item">
<UrlAction Url="" />
</CustomAction>
我希望这段代码用“我的Url”更新UrlAction Url,但它没有。如果我用XML硬编码一个URL,它可以工作,但我必须能够通过编程实现。根据您想要实现的目标,您可以使用一些javascript
<UrlAction Url="JavaScript:window.location='{SiteUrl}/_layouts/CustomListAction.aspx?ID={ListId}'"/>
~site和~siteCollection也起作用:
<UrlAction Url="~site/_layouts/Page.aspx?ID={ListId}"/>
我认为WSS模式定义不允许在UrlAction元素中使用空Url属性。也许可以尝试在xml中添加一个“默认”url,以便稍后覆盖?您可以在SPWeb对象上使用SPUserCustomActionCollection:
using (SPSite site = new SPSite("http://moss.dev.com"))
using (SPWeb web = site.OpenWeb())
{
SPContentType contentType = web.ContentTypes["Curriculum Vitae"];
SPUserCustomAction action = web.UserCustomActions.Add();
action.RegistrationType = SPUserCustomActionRegistrationType.ContentType;
action.RegistrationId = contentType.Id.ToString();
action.Location = "EditControlBlock";
action.Sequence = 450;
action.Title = "Test";
action.Rights = SPBasePermissions.EditListItems;
action.Url = "http://www.google.com";
action.Update();
}
这样,您可以将URL设置为您想要的任何内容。如果要更新现有的自定义操作,则可以遍历集合并更新要查找的操作。安装自定义操作后更新元素XML定义没有任何作用。很遗憾ECB自定义不支持ControlClass,这将比旧的{SiteUrl}宏提供更大的灵活性代码>,或该操作在子网站中不可见。
using (SPSite site = new SPSite("http://moss.dev.com"))
using (SPWeb web = site.OpenWeb())
{
SPContentType contentType = web.ContentTypes["Curriculum Vitae"];
SPUserCustomAction action = web.UserCustomActions.Add();
action.RegistrationType = SPUserCustomActionRegistrationType.ContentType;
action.RegistrationId = contentType.Id.ToString();
action.Location = "EditControlBlock";
action.Sequence = 450;
action.Title = "Test";
action.Rights = SPBasePermissions.EditListItems;
action.Url = "http://www.google.com";
action.Update();
}