Javascript 使用JSOM在SharePoint列表功能区按钮中添加按钮

Javascript 使用JSOM在SharePoint列表功能区按钮中添加按钮,javascript,sharepoint,ecmascript-5,csom,Javascript,Sharepoint,Ecmascript 5,Csom,我必须在Sharepoint列表功能区按钮和ECB菜单中添加一个按钮。 下面是使用JSOM在Sharepoint列表中添加ECB菜单的代码 function AddCustomActions() { var listTitle = 'mylist Title'; var clientContext = new SP.ClientContext(); var oWebsite = clientContext.get_web()

我必须在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在功能区按钮中添加按钮吗?

这是位置属性,下面是msdn中的列表

例如:

 newUserCustomAction.set_location('NewFormToolbar');
更新:

Tbh如果您想要最大限度地控制,您应该使用
set_commandUIExtension
,您可以在部署自定义操作时使用相同的标记

可能是这样的:

var context = new SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle("stackoverflow")
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 stackoverflow\');" />'+
                    '</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());
});
var context=new SP.ClientContext.get_current();
var list=context.get_web().get_list().getByTitle(“stackoverflow”)
var customAction=list.get_userCustomActions().add();
customAction.set_位置('CommandUI.Ribbon.ListView');
变量uiExtension=''+
'' +
''+
'' +
''+
''+
''+
''+
''+
'';
customAction.set_commandUIExtension(uiExtension)
customAction.update();
load(列表,'UserCustomActions');
executeQueryAsync(函数(){
控制台日志(“成功”);
},
函数(发送方,参数){
console.log(args.get_message());
});

这是位置属性,下面是msdn的列表

例如:

 newUserCustomAction.set_location('NewFormToolbar');
更新:

Tbh如果您想要最大限度地控制,您应该使用
set_commandUIExtension
,您可以在部署自定义操作时使用相同的标记

可能是这样的:

var context = new SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle("stackoverflow")
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 stackoverflow\');" />'+
                    '</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());
});
var context=new SP.ClientContext.get_current();
var list=context.get_web().get_list().getByTitle(“stackoverflow”)
var customAction=list.get_userCustomActions().add();
customAction.set_位置('CommandUI.Ribbon.ListView');
变量uiExtension=''+
'' +
''+
'' +
''+
''+
''+
''+
''+
'';
customAction.set_commandUIExtension(uiExtension)
customAction.update();
load(列表,'UserCustomActions');
executeQueryAsync(函数(){
控制台日志(“成功”);
},
函数(发送方,参数){
console.log(args.get_message());
});

您面临的错误是什么?我不知道如何使用ECMAScript/JSOM在SharePoint列表功能区按钮中添加按钮。这是添加ECB菜单的代码,工作正常。您面临的错误是什么?我不知道如何使用ECMAScript/JSOM在SharePoint列表功能区按钮中添加按钮。这是添加ECB菜单的代码,工作正常。谢谢,但不工作,我必须更改这些行吗<代码>var UserCustomActions=oList.get_UserCustomActions();var newUserCustomAction=UserCustomActions.add()已尝试
查看工具栏
但它没有添加到列表工具->项目任务中但它不工作我必须更改这些行吗<代码>var UserCustomActions=oList.get_UserCustomActions();var newUserCustomAction=UserCustomActions.add()已尝试
查看工具栏
,但未在列表工具->项目中添加