3

I created a custom action in the ribbon by VS. I see the button always. Also when no list items are selected. How can I enable the button only when 1 or more listitems are selected?

This is my current custom action code:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="85691508-c076-4f43-93d4-96b4d5253a09.RibbonCustomAction1"
                RegistrationType="ContentType"
                RegistrationId="0x0101000ACB09B019144AB4A7A9EDD1CCCC4ED3"
                Location="CommandUI.Ribbon"
                Sequence="10001"
                Rights="AddListItems"
                Title="Invoke &apos;RibbonCustomAction1&apos; action">
    <CommandUIExtension>
      <!-- 
      Update the UI definitions below with the controls and the command actions
      that you want to enable for the custom action.
      -->
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.Documents.Manage.Controls._children">
          <Button Id="Ribbon.Documents.Manage.RibbonCustomAction1Button"
                  Alt="Archive"
                  Sequence="100"
                  Command="Invoke_RibbonCustomAction1ButtonRequest"
                  LabelText="Archive"
                  TemplateAlias="o1"
                  Image32by32="_layouts/15/images/placeholder32x32.png"
                  Image16by16="_layouts/15/images/placeholder16x16.png" />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler Command="Invoke_RibbonCustomAction1ButtonRequest"
                          CommandAction="javascript:alert(SP.ListOperation.Selection.getSelectedItems().length);" />
      </CommandUIHandlers>
    </CommandUIExtension >
  </CustomAction>
</Elements> 
5

Solution in 1 line: EnabledScript="javascript:SP.ListOperation.Selection.getSelectedItems().length > 0;"

  • 1
    To complete the answer: EnabledScript is an attribute of CommandUIHandler, just like CommandAction. – dbardakov Jul 8 '17 at 7:13
2

Use EnabledScript property in CommandUIHandler. Example:

 EnabledScript="javascript:
function enableButton() {
    var items = SP.ListOperation.Selection.getSelectedItems();
    var ci = CountDictionary(items);
    if (ci > 0) {
        return true;
    }
}
enableButton();"
0

Please try this code - I am able to check if any items are selected or not by attaching a click handler to the class .s4-itm-cbx (SP 2013)

$(".s4-itm-cbx").click(function(){

//console.log(SP.ListOperation.Selection.getSelectedItems().length);
if(!SP.ListOperation.Selection.getSelectedItems().length)
{
 //code to enable/disable ribbon button


 $(".ms-cui-tabBody li:nth-child(5)  .ms-cui-row:nth-child(2)").css({display:'none'})
    }




})
0

Your commandUIHandler has to look like the following

<CommandUIHandlers>
    <CommandUIHandler Command="Invoke_RibbonCustomAction1ButtonRequest"
      EnabledScript="javascript:
        function enableButton() {
            var items = SP.ListOperation.Selection.getSelectedItems();
            var ci = CountDictionary(items);
            if (ci > 0) {
                return true;
            }
        }
        enableButton();"
       CommandAction="javascript:alert(SP.ListOperation.Selection.getSelectedItems().length);" />
  </CommandUIHandlers>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.