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

8 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>
Run Code Online (Sandbox Code Playgroud)

在我的功能接收器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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望此代码能够使用"我的网址"更新UrlAction网址,但事实并非如此.如果我在XML中硬编码URL它可以工作,但我必须能够以编程方式执行.

Cha*_*hen 7

您可以在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();
        }
Run Code Online (Sandbox Code Playgroud)

这样,您可以将URL设置为您想要的任何内容.如果要更新现有的自定义操作,则可以遍历该集合并更新要查找的集合.安装自定义操作后更新元素XML定义不会执行任何操作.


Fra*_*ino 2

根据你想要实现的目标,你可以使用一些 javascript;

<UrlAction Url="JavaScript:window.location='{SiteUrl}/_layouts/CustomListAction.aspx?ID={ListId}'"/>
Run Code Online (Sandbox Code Playgroud)

~site 和 ~siteCollection 也可以工作:

<UrlAction Url="~site/_layouts/Page.aspx?ID={ListId}"/>
Run Code Online (Sandbox Code Playgroud)