Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

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

share|improve this answer
    
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 '11 at 8:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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