How to disable a ribbon button when a folder is selected
-
16-10-2019 - |
Question
I have added a new button to the SharePoint ribbon for document libraries. This all works well and the button is enabled / disabled depending on the number of items selected... fine!
I want to improve this by disabling the button when a folder is selected similar to how the standard SharePoint 'email a link' button works. Can anyone offer any advice on how to approach this? I have Googled around, but have not been able to find anything.
The code I currently have is as follows:
<CustomAction
Id="Muhimbi.SharePoint.DocumentConverter.PDF.Ribbon.Documents.Copies.Controls.ConvertAndDownload.Action"
Location="CommandUI.Ribbon"
RegistrationType="ContentType"
RegistrationId="0x0101"
>
<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition
Location="Ribbon.Documents.Copies.Controls._children">
<Button Id="Muhimbi.SharePoint.DocumentConverter.PDF.Ribbon.Documents.Copies.Controls.ConvertAndDownload.Button"
Command="Muhimbi.SharePoint.DocumentConverter.PDF.Ribbon.Documents.Copies.Controls.ConvertAndDownload.Button.Command"
Image16by16="/_layouts/images/Muhimbi.PDFConverter/pdf16.gif"
Image32by32="/_layouts/images/Muhimbi.PDFConverter/pdf32.gif"
LabelText="$Resources:ecb_title;"
Sequence="12"
TemplateAlias="o1" />
</CommandUIDefinition>
</CommandUIDefinitions>
<CommandUIHandlers>
<CommandUIHandler
Command="Muhimbi.SharePoint.DocumentConverter.PDF.Ribbon.Documents.Copies.Controls.ConvertAndDownload.Button.Command"
CommandAction="javascript:window.location='{SiteUrl}/_layouts/Muhimbi.PDFConverter/Convert.aspx?action=ConvertAndDownload&ListId={ListId}&ItemId=' + SP.ListOperation.Selection.getSelectedItems()[0].id + '&Source=' + window.location"
EnabledScript="javascript:function singleEnable()
{
var items = SP.ListOperation.Selection.getSelectedItems();
var ci = CountDictionary(items);
return (ci == 1);
}
singleEnable();" />
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>
Solution
Use the fsObjType
property:
EnabledScript="javascript:
var items = SP.ListOperation.Selection.getSelectedItems();
(items.length == 1 && items[0].fsObjType == 0);"
OTHER TIPS
Can u alert what do you get when you select a folder using SP.ListOperation.Selection.getSelectedItems();
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange