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

时间:2015-03-11 10:27:51

标签: javascript sharepoint ecmascript-5 csom

我必须在Sharepoint List Ribbon按钮&中添加一个按钮。在ECB菜单中。 这是使用JSOM在Sharepoint List中添加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);
        }
    }

enter image description here 任何人都可以帮助如何使用JSOM在Ribbon Button中添加按钮吗?

1 个答案:

答案 0 :(得分:3)

它是位置属性,这是来自msdn的default locations列表

示例:

 newUserCustomAction.set_location('NewFormToolbar');

更新:

如果您想要最大程度的控制,您应该使用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());
});