如果我选择一个特定的列表来添加操作,我就会实现这一点。有没有一种简单的方法可以在整个网站集中的所有文档库上启用此自定义操作?
代码示例:
function createUserCustomActionList() {
var cmd = "<CommandUIExtension><CommandUIDefinitions><CommandUIDefinition Location="Ribbon.Documents.Manage.Controls._children">" +
"<Button Id="DiaryAction.Button" TemplateAlias="o1" Command="DiaryCommand" CommandType="General" LabelText="Dela flera" Image32by32="https://eolusvind.sharepoint.com/sites/intranet/_layouts/15/1033/Images/formatmap32x32.png?rev=23"" +
" Image32by32Top="-271" Image32by32Left="-109" />" +
"</CommandUIDefinition></CommandUIDefinitions><CommandUIHandlers>" +
"<CommandUIHandler Command ="DiaryCommand" CommandAction="javascript:alert('Hej');" EnabledScript="javascript:SP.ListOperation.Selection.getSelectedItems().length > 1;" />" +
"</CommandUIHandlers></CommandUIExtension>";
var ctx = new SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle('Dokument');
var uca = list.get_userCustomActions();
var oUserCustomAction = uca.add();
oUserCustomAction.set_location('CommandUI.Ribbon.ListView');
oUserCustomAction.set_commandUIExtension(cmd);
oUserCustomAction.set_sequence(100);
oUserCustomAction.set_title('Dela flera');
oUserCustomAction.update();
ctx.load(list, 'Title' ,'UserCustomActions');
ctx.executeQueryAsync(function () {
alert('Custom action created for ' + list.get_title())
}, function (sender, args) {
alert('Request failed. ' + args.get_message() + 'n' + args.get_stackTrace());
});
}
我建议使用以下方法在整个网站集的文档库中注册自定义按钮:
- 使用设置为2的
RegistrationType
(ContentType)和设置为0x0101
的RegistrationId
(用于Document
内容类型)通过内容类型注册自定义操作 - 使用站点范围用户自定义操作在所有站点中应用更改集合
示例
var cmd = "<CommandUIExtension>" +
"<CommandUIDefinitions>" +
"<CommandUIDefinition Location="Ribbon.Documents.Manage.Controls._children">" +
"<Button Id="ClickAction.Button" TemplateAlias="o1" Command="ViewCustomPropertiesCommand" CommandType="General" LabelText="View Custom Properties" Image32by32="/_layouts/15/1033/Images/formatmap32x32.png?rev=23" Image32by32Top="-1" Image32by32Left="-171" />" +
"</CommandUIDefinition></CommandUIDefinitions><CommandUIHandlers>" +
"<CommandUIHandler Command ="ViewCustomPropertiesCommand" CommandAction="javascript:console.log('View Custom Properties');" EnabledScript="javascript:SP.ListOperation.Selection.getSelectedItems().length > 0;" />" +
"</CommandUIHandlers>" +
"</CommandUIExtension>";
var ctx = new SP.ClientContext.get_current();
var customAction = ctx.get_site().get_userCustomActions().add(); //1. apply via site scope user custom action
customAction.set_location('CommandUI.Ribbon.ListView');
customAction.set_commandUIExtension(cmd);
customAction.set_sequence(112);
customAction.set_registrationType(2); //2.set to ContentType
customAction.set_registrationId("0x0101"); //2.set Document content type
//customAction.set_title('Document custom button');
customAction.update();
ctx.executeQueryAsync(function () {
console.log('Button has been registered');
}, function (sender, args) {
console.log('Request failed. ' + args.get_message() + 'n' + args.get_stackTrace());
});