Andrew Connel explains how to enable and disable buttons in the Ribbon based on a condition.
I need to check a field value on the selected item and the item permission and then enable / disable multiple buttons. I assume that you have some knowledge about the Ribbon XML structure :).
//Should only be enabled when IssueStatus == Report
<CommandUIHandler
Command="Tab.CloseIssue"
CommandAction="javascript:EditDeviation('{ListId}');"
EnabledScript="javascript:SingleEnable('Report',SP.PermissionKind.editListItems);"/>
//Should always be enabled
<CommandUIHandler
Command="Tab.CurrentStatus"
CommandAction="javascript:DisplayStatus('{ListId}');"
EnabledScript="javascript:SingleEnable('All',,SP.PermissionKind.viewListItems);"/>
function SingleEnable(EnableForStatus, PermissionKind) {
var result = false;
var selectedItems = SP.ListOperation.Selection.getSelectedItems();
var ci2 = CountDictionary(selectedItems);
if (ci2 == 1) {
if (this.IssueRibbonItemId == null) {
this.IssueRibbonItemId = selectedItems[0]['id'];
var listGuid = SP.ListOperation.Selection.getSelectedList();
getStatus(this.IssueRibbonItemId, listGuid);
}
else if (this.IssueRibbonItemId != selectedItems[0]['id']) {
this.IssueRibbonItemId = selectedItems[0]['id'];
var listGuid = SP.ListOperation.Selection.getSelectedList();
getStatus(this.IssueRibbonItemId, listGuid);
}
else if(this.IssueRibbonStatus != null){
if (this.IssueRibbonStatus == EnableForStatus || EnableForStatus == '') {
if(this.IssueItem.get_effectiveBasePermissions().has(PermissionKind)){
result = true;
}
}
}
}
return result;
}
function getStatus(ItemId, listGuid) {
var clientContext = new SP.ClientContext();
var web = clientContext.get_web();
var lists = web.get_lists();
var list = lists.getById(listGuid);
this.IssueItem = list.getItemById(ItemId);
clientContext.load(IssueItem, 'IssueStatus', 'EffectiveBasePermissions');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onStatusQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onStatusQuerySucceeded(sender, args) {
this.IssueRibbonStatus = this.IssueItem.get_item("IssueStatus");
RefreshCommandUI(); //Makes the EnabledScrip method to be enabled again.
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}