sharepoint - 如何在 SharePoint 中以编程方式添加自定义菜单操作?

标签 sharepoint content-type custom-action

我需要在 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/

相关文章:

c# - 如何使用 Sharepoint Web 服务从 .NET 获取文档列表?

Sharepoint ListView 设置

python - Django:每个外键返回一个过滤对象

apache - HTML5 缓存 list 和内容类型

node.js - Apache、 Node 、.htaccess

c# - Wix:在自定义操作中写入文件

WiX 对主要升级回滚的自定义操作未运行

wix - 如何使用 WiX 记录自定义操作?

SharePoint 快速启动和顶部导航栏消失

sharepoint - 将 Microsoft Graph 中的 token 与 Sharepoint rest api 结合使用