My prevoius posts(here and here) described how to create a custom ribbon action and how to retrieve selected list items in SharePoint 2010. Right now I’ll show You one more thing that can be usefull while creating Your own ribbon buttons – how to enable or disable ribbon button using defined by You conditions.
“EnabledScript” is a property of CommandUIHandler. It allows You to set if the ribbon button should be enabled(available to click for the user) or disabled(greyed out). To set this property dynamically You can use the script shown below:
var EnableDisable = function() {
this.clientContext = SP.ClientContext.get_current();
this.selectedItems = SP.ListOperation.Selection.getSelectedItems(this.clientContext);
var ci = CountDictionary(selectedItems);
return (ci > 0);
};
EnableDisable();
In the script I check if user has selected one or more list items. If yes, then method returns true, otherwise false.
Full command should be placed in the elements.xml and should look like this:
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="CAAlertRibbonButton"
RegistrationId="100"
RegistrationType="List"
Location="CommandUI.Ribbon"
Sequence="5"
Title="Alert Ribbon Button">
<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition Location="Ribbon.List.Settings.Controls._children">
<Button
Id="AlertRibbonButtonId"
Alt="Alert Ribbon Button"
Sequence="5"
Command="CMDAlertRibbonButton"
Image32by32="/_layouts/images/menulistsettings.gif"
Image16by16="/_layouts/images/itdcl.gif"
LabelText="Alert Ribbon Button"
TemplateAlias="o1" />
</CommandUIDefinition>
</CommandUIDefinitions>
<CommandUIHandlers>
<CommandUIHandler
Command="CMDAlertRibbonButton"
EnabledScript="javascript: var EnableDisable = function() {
this.clientContext = SP.ClientContext.get_current();
this.selectedItems = SP.ListOperation.Selection.getSelectedItems(this.clientContext);
var ci = CountDictionary(selectedItems);
return (ci > 0);
};
EnableDisable();"
CommandAction="javascript: alert('My Command!');" />
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>
</Elements>

May 11th, 2012 on 11:21
This is brilliant, exactly what I was looking for! Thanks
Now you wouldn’t happen to know how to go about it if I want to check if a field with a certain title exists on that list in order to enable it? I tried it, but using clientContext.executeQueryAsync seems to break and grays out all the ribbon buttons?
Thanks again!
May 30th, 2012 on 08:13
To be honest I don’t have any fast solution. Maybe if I find sometime I’ll look on to it closer.
June 8th, 2012 on 09:00
thankful Tomasz … your code is Working !!!
June 20th, 2012 on 17:39
Excelent!!! that was exactly what i was looking for.
Thanks!!
July 4th, 2012 on 11:13
you don’t need to write your own mini-function. how about:
EnabledScript=”javascript:SP.ListOperation.Selection.getSelectedItems().length > 0;”
August 22nd, 2012 on 15:56
This is driving me nuts. I always get a value of 0, even when I am viewing my button in the Display or Edit view of an item. Shouldn’t I be getting a length of 1 when I have an item selected?