使用 C# CSOM 创建 UserCustomAction

Create UserCustomAction with C# CSOM

正在使用 SharePoint 2013。尝试将 UserCustomAction 添加到 EditForm。

            List custom = web.Lists.GetByTitle("custom");
        UserCustomActionCollection UserActions = custom.UserCustomActions;
        ctx.Load(UserActions);
        ctx.ExecuteQuery();
        UserCustomAction CreateEmailAction = custom.UserCustomActions.Add();
        CreateEmailAction.RegistrationId = "{0BF934CE-0175-4E9D-BA6A-F58B7B1F2A89}";
        CreateEmailAction.RegistrationType = UserCustomActionRegistrationType.List;
        CreateEmailAction.Title = "HT Edit form Create email";
        CreateEmailAction.Location = "CommandUI.Ribbon.EditForm";
        CreateEmailAction.Group = "Actions";
        CreateEmailAction.Sequence = 1000;
        CreateEmailAction.CommandUIExtension = Properties.Resources.ribbon_editmeeting;
        CreateEmailAction.Update();
        ctx.ExecuteQuery();

XML代码:

<CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">

  <Button Id="Ribbon.Documents.New.RibbonTest"
          Command="Notify"
          Sequence="0"
          Image16by16="/_layouts/images/NoteBoard_16x16.png"
          Image32by32="/_layouts/images/NoteBoard_32x32.png"
          Description="Uses the notification area to display a message."
          LabelText="Notify hello"
          TemplateAlias="o1"/></CommandUIDefinition></CommandUIDefinitions>

失败,出现错误“Microsoft.SharePoint.Client.ServerException” HResult=0x80131500 Message=不支持设置 属性。 {8F405B73-81C3-47C9-A07F-8899B57F09F9} 的值已经设置,无法更改。'

如果我省略 RegistrationID 行,代码会成功完成,但不会创建任何操作。

1) RegistrationID 指的是动作将被注册到的列表。由于您要通过以下行添加自定义操作:

    UserCustomAction CreateEmailAction = custom.UserCustomActions.Add();

(自定义引用列表)该部分已为您完成。通过尝试重置值,您可以解决错误。

2) 使用 "List Item Menu" 的位置操作有效:

    UserCustomActionCollection UserActions = custom.UserCustomActions;
    ctx.Load(UserActions);
    ctx.ExecuteQuery();
    UserCustomAction CreateEmailAction = custom.UserCustomActions.Add();

    CreateEmailAction.Title = "HT Edit form Create email";

    CreateEmailAction.Location = "EditControlBlock";

    CreateEmailAction.Group = "Actions";
    CreateEmailAction.Sequence = 1000;
    CreateEmailAction.Update();
    ctx.ExecuteQuery();

所以我认为第二部分(如果未创建)与创建 UI 以触发按钮有关。可能有更多的资源来给编辑表单添加一个按钮 这里:https://sharepoint.stackexchange.com/q/103696 和 这里:https://sharepoint.stackexchange.com/q/133088

3) 自定义动作基本上调用了一个URL,所以你还需要设置要激活的动作的URL。这也可能是一个工作流程。例如:

    sendDocumentAction.Url = String.Format("~site/_layouts/15/wfstart.aspx?List={{ListId}}&ID={{ItemId}}&SubscriptionID={0}", newSubscription.Id);

其中 newSubscription 是要激活的工作流订阅