Conditionally Enable/Disable SharePoint 2010/2013 Ribbon button (Check selected item is folder or file)

Today i have faced one scenario, Need to validate whether the selected item is folder or file through custom action. i have tried many ways finally i got below solution. its working fine for both SP2010 and 2013.

hope it will help you.Happy coding....

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
      Id="Review.Link"
      RegistrationType="List"
      RegistrationId="101"
      Location="EditControlBlock"
      Sequence="350"
      ImageUrl="../_layouts/15/images/Images/revi-32.jpg"
      Title="Review"
     Description="Items for reviews">
    <UrlAction Url="javascript:
                 var  ctx= SP.ClientContext.get_current();
              var IDs='';
             
              var items1 = SP.ListOperation.Selection.getSelectedItems(ctx);
              for (item in items1) {
                if (items1[item].fsObjType != 1){
                alert('Please select the folder!!!');
                return false;
                }
              }
                var options = { 
                url: '/_layouts//Review.aspx?List={ListId}&amp;ID={ItemId}',
                tite: 'Review',
                allowMaximize:true,
                showClose:true,
                width: 500,
                height: 400,
                                                };  
        
              SP.UI.ModalDialog.showModalDialog(options);"/>
  </CustomAction>

  <CustomAction
       Id="Review.Link"
        RegistrationType="List"
        RegistrationId="101"
        Location="CommandUI.Ribbon"
        Sequence="5"
        Title="Review">


    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.Documents.Manage.Controls._children">
          <Button
              Id="Ribbon.Documents.New.Review.Link"
              Alt="Review"
              Sequence="5"
              Command="Review"
              LabelText="Review"
              Image32by32="../_layouts/15/images/Images/revi-32.jpg"
              TemplateAlias="o1"/>
        </CommandUIDefinition>

      </CommandUIDefinitions>

      <CommandUIHandlers>
        <CommandUIHandler
          Command="Review"
          CommandAction="javascript:
               var  ctx= SP.ClientContext.get_current();
              var IDs='';
             
              var items1 = SP.ListOperation.Selection.getSelectedItems(ctx);
              for (item in items1) {
                if (items1[item].fsObjType != 1){
                alert('Please select the folder!!!');
                return false;
                }
              }
               for (var i in items1) {
                 IDs +=  items1[i].id;
               }
              
         
              var options = {
              
                url: '{SiteUrl}/_layouts/Review.aspx?IsDlg=1&amp;List=' + SP.ListOperation.Selection.getSelectedList()+'&amp;ID='+IDs+'&amp;SiteURL=' + window.location,
                tite: 'Size Limit',
                allowMaximize: true,
                showClose: true,
                width: 500,
                height: 400
                };
             
              SP.UI.ModalDialog.showModalDialog(options);"
                             EnabledScript="javascript:function singleEnable()
          {
            var items = SP.ListOperation.Selection.getSelectedItems();
            var ci = CountDictionary(items);
            
            return (ci == 1);
          }
          singleEnable();" />

      </CommandUIHandlers>

    </CommandUIExtension>

  </CustomAction>


</Elements>

More Info :Reference

Comments

Popular posts from this blog

IRM and the Object Model

This content database has a schema version which is not supported in this farm

Activate and Deactivate Feature through PowerShell