我需要在 c# 中以编程方式将自定义菜单操作添加到自定义内容类型。这是因为我不会事先知道需要链接到的 URL。激活该功能时,将从配置中提取链接到的 URL。
我尝试了以下方法:
在我的 Element.xml 文件中添加 CustomAction 为:
<CustomAction
Id="MyID"
RegistrationType="ContentType"
RegistrationId="0x010100ef19b15f43e64355b39431399657766e"
Location="EditControlBlock"
Sequence="1000"
Title="My Menu Item">
<UrlAction Url="" />
</CustomAction>
在我的功能接收器 FeatureActivated 方法中,我有:
SPElementDefinitionCollection eleCollection =
properties.Feature.Definition.GetElementDefinitions(
new System.Globalization.CultureInfo(1));
foreach (SPElementDefinition ele in eleCollection)
{
if (ele.Id == "MyID")
{
System.Xml.XmlNode node = ele.XmlDefinition.FirstChild;
node.Attributes[0].Value = "MY URL";
ele.FeatureDefinition.Update(true);
}
}
我希望此代码使用“我的 URL”更新 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 定义不会执行任何操作。
关于sharepoint - 如何在 SharePoint 中以编程方式添加自定义菜单操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1462899/