Search This Blog

Thursday, May 12, 2016

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

Objective:

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

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

<CommandUIHandlers>
        <CommandUIHandler
                Command="CommandName"
                CommandAction=" … "
                EnabledScript=" …  "/>
      </CommandUIHandlers>
</CommandUIHandlers>
Add the following JavaScript code in EnabledScript:

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

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