使用 JSOM 在 SharePoint 列表功能区按钮中添加按钮
Add Button in SharePoint List Ribbon Button Using JSOM
我必须在 Sharepoint 列表功能区按钮和 ECB 菜单中添加一个按钮。
这是使用 JSOM 在 Sharepoint 列表中添加 ECB 菜单的代码。
function AddCustomActions() {
var listTitle = 'mylist Title';
var clientContext = new SP.ClientContext();
var oWebsite = clientContext.get_web();
var oList = oWebsite.get_lists().getByTitle(listTitle);
var UserCustomActions = oList.get_userCustomActions();
var newUserCustomAction = UserCustomActions.add();
newUserCustomAction.set_location('EditControlBlock');
newUserCustomAction.set_url("javascript:archieveItem('{ListId}','{ItemId}');");
newUserCustomAction.set_sequence(3);
newUserCustomAction.set_title('Archive Item');
newUserCustomAction.set_description('');
newUserCustomAction.update();
clientContext.executeQueryAsync(onQuerySucceed, onQueryFail);
}
function onQuerySucceed(sender, args) {
alert('New custom action added to Site.\n\nRefresh the page.');
}
function onQueryFail(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
function archieveItem(aListId, aItemId) {
try {
var flag1 = confirm('Are you sure want to archive the Item? Click Ok to Archive and Cancel to Unarchive');
var clientContext = SP.ClientContext.get_current();
var oWeb = clientContext.get_web();
var id = aItemId;
var listId = aListId;
var oList = oWeb.get_lists().getByTitle(listTitle);
var oListItem = oList.getItemById(id);
if (flag1 == true) {
oListItem.set_item('Archive', true);
}
else if (flag1 == false) {
oListItem.set_item('Archive', false);
}
oListItem.update();
clientContext.executeQueryAsync(onArchiveSucceeded, onArchiveFailed);
}
catch (e) {
alert(e.message);
}
}
谁能帮助如何使用 JSOM 在功能区按钮中添加按钮?
这是位置 属性,这是来自 msdndefault locations 的列表
示例:
newUserCustomAction.set_location('NewFormToolbar');
更新:
Tbh 如果您想要最大程度的控制,您应该使用 set_commandUIExtension
,您可以在部署自定义操作时使用与您相同的标记。
可能是这样的:
var context = new SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle("Whosebug")
var customAction = list.get_userCustomActions().add();
customAction.set_location('CommandUI.Ribbon.ListView');
var uiExtension = '<CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">' +
'<CommandUIDefinitions>' +
'<CommandUIDefinition Location="Ribbon.List.CustomizeList.Controls._children">'+
'<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>'+
'<CommandUIHandlers>'+
'<CommandUIHandler Command="Notify" '+
'CommandAction="javascript:SP.UI.Notify.addNotification(\'Hello Whosebug\');" />'+
'</CommandUIHandlers>'+
'</CommandUIExtension>';
customAction.set_commandUIExtension(uiExtension)
customAction.update();
context.load(list,'UserCustomActions');
context.executeQueryAsync(function() {
console.log("success");
},
function(sender, args) {
console.log(args.get_message());
});
我必须在 Sharepoint 列表功能区按钮和 ECB 菜单中添加一个按钮。 这是使用 JSOM 在 Sharepoint 列表中添加 ECB 菜单的代码。
function AddCustomActions() {
var listTitle = 'mylist Title';
var clientContext = new SP.ClientContext();
var oWebsite = clientContext.get_web();
var oList = oWebsite.get_lists().getByTitle(listTitle);
var UserCustomActions = oList.get_userCustomActions();
var newUserCustomAction = UserCustomActions.add();
newUserCustomAction.set_location('EditControlBlock');
newUserCustomAction.set_url("javascript:archieveItem('{ListId}','{ItemId}');");
newUserCustomAction.set_sequence(3);
newUserCustomAction.set_title('Archive Item');
newUserCustomAction.set_description('');
newUserCustomAction.update();
clientContext.executeQueryAsync(onQuerySucceed, onQueryFail);
}
function onQuerySucceed(sender, args) {
alert('New custom action added to Site.\n\nRefresh the page.');
}
function onQueryFail(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
function archieveItem(aListId, aItemId) {
try {
var flag1 = confirm('Are you sure want to archive the Item? Click Ok to Archive and Cancel to Unarchive');
var clientContext = SP.ClientContext.get_current();
var oWeb = clientContext.get_web();
var id = aItemId;
var listId = aListId;
var oList = oWeb.get_lists().getByTitle(listTitle);
var oListItem = oList.getItemById(id);
if (flag1 == true) {
oListItem.set_item('Archive', true);
}
else if (flag1 == false) {
oListItem.set_item('Archive', false);
}
oListItem.update();
clientContext.executeQueryAsync(onArchiveSucceeded, onArchiveFailed);
}
catch (e) {
alert(e.message);
}
}
这是位置 属性,这是来自 msdndefault locations 的列表
示例:
newUserCustomAction.set_location('NewFormToolbar');
更新:
Tbh 如果您想要最大程度的控制,您应该使用 set_commandUIExtension
,您可以在部署自定义操作时使用与您相同的标记。
可能是这样的:
var context = new SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle("Whosebug")
var customAction = list.get_userCustomActions().add();
customAction.set_location('CommandUI.Ribbon.ListView');
var uiExtension = '<CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">' +
'<CommandUIDefinitions>' +
'<CommandUIDefinition Location="Ribbon.List.CustomizeList.Controls._children">'+
'<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>'+
'<CommandUIHandlers>'+
'<CommandUIHandler Command="Notify" '+
'CommandAction="javascript:SP.UI.Notify.addNotification(\'Hello Whosebug\');" />'+
'</CommandUIHandlers>'+
'</CommandUIExtension>';
customAction.set_commandUIExtension(uiExtension)
customAction.update();
context.load(list,'UserCustomActions');
context.executeQueryAsync(function() {
console.log("success");
},
function(sender, args) {
console.log(args.get_message());
});