开发者

SharePoint 2010 - enable custom ribbon button only if user has permission to edit selected item

I have a custom action in a ribbon, and I need to enable the button only if current user has permission to edit the item (Contribute role). I have a PageComponent to tell the UI if command can be handled, but I can figure out how to check user permissions for an item in javascript.

This is in my PageComponent:

   开发者_StackOverflow     canHandleCommand: function (commandId) {
            switch (commandId) {
                case 'Command1':
                    var ids = getSelectedIds(); // gets an array of selected ids

                    var selectionChanged = false;
                    if (ids.length != this.previousIds.length) {
                        selectionChanged = true;
                    } else {
                        for (var index in ids) {
                            if (ids[index] != this.previousIds[index]) {
                                selectionChanged = true;
                            }
                        }
                    }

                    if (selectionChanged) {
                        this.enabledStatusChecked = false;
                    }

                    this.previousIds = ids;

                    if (!this.enabledStatusChecked) {
                        this.checkIsEnabled(ids);
                    }

                    return this.isEnabled;
            }

            return false;
        },
        checkIsEnabled: function (ids) {
            this.enabledStatusChecked = true;
            this.isEnabled = false;

            if (ids.length != 1) {
                return;
            }

            var id = ids[0];

            var context = SP.ClientContext.get_current();
            var web = context.get_web();

            var list = web.get_lists().getById(SP.ListOperation.Selection.getSelectedList());
            var item = list.getItemById(id);

            context.load(item);
            context.executeQueryAsync(Function.createDelegate(this, function () {
                var contentTypeId = item.get_item('ContentTypeId').toString();
                if (!contentTypeId.lastIndexOf(Constants.InternalNormContentTypeId, 0)) {
                    this.isEnabled = true;
                    // !! need to check permissions here !!
                }
                RefreshCommandUI();
            }), Function.createDelegate(this, function () {
                RefreshCommandUI();
            }));
        },

This code enables the button only if 1 item is selected and if it is of specified content type. Does anybody have any idea about how to check permission through javascript?


from my point of view. You have two ways, the first adding tag in html/master page: http://buyevich.blogspot.com/2010/08/hide-ribbon-from-visitorsanonimus-users_31.html or created asp control and also take it to page: http://dicemastersharespoint.blogspot.com/2011/02/hiding-buttonscontrols-on-sharepoint.html

Write back whether possible for your solution or not.

Best Regards

Martin

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜