Thursday, April 28, 2011

Enable Ribbon button when one item only is selected and it is not folder

To programmatically enable a ribbon button by JavaScript, when one item only is selected.

Edit the element.xml file of the Ribbon button Custom Action.
In the CommandUIHandles section

                EnabledScript=" …  "/>

Add the following JavaScript code in EnabledScript:

        function enable() {
        var items = SP.ListOperation.Selection.getSelectedItems();
        var itemCount = CountDictionary(items);
        return (itemCount==1 &amp;&amp; items[0].fsObjType == 0);

itemCount==1  is used to identify if one item only is selected

If this condition is TRUE it means that the “items” array contains the element 0.
So it is possible to test if is it a folder with the following:

 items[0].fsObjType == 0

If you write you JavaScript code in the Element.xml file, you need to encode the operator AND: &amp;&amp;

No comments:

Post a Comment