0

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). The action is defined in a document library, the way it should work is that user selects (highlights) an item and if he has edit permission the button should be enabled (much like 'Edit item' built in button). Also keep in mind that items can have different permissions.

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:

        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?

1 Answer 1

3

CustomAction has Rights attribute which accepts the permission levels. SharePoint should take care of disabling it if user does not have permission that you set.

http://msdn.microsoft.com/en-us/library/ms460194.aspx

2
  • That doesn't work with the rest of the page component code. I also need to check the content type of selected item and count of selected items. Also, I'm not sure whether it would work on per item basis.
    – Necros
    Jul 20, 2011 at 8:04
  • What if I want permissions for different object? How can I specify list for those permissions? (Eg. workflow that work on 2 lists, and want to check 2nd list permissions)
    – przemo_li
    Nov 27, 2014 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.