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?