SharePoint 2010 Disable custom action button if selected item is a folder

In this post i would like to share how you can distinguish between an item and a folder if you are using custom actions and want to enable or disable your custom action based on the selection of the user ( item / folder).

Custom actions in SharePoint are really useful in order to use the SharePoint UI and extend it with your own actions and functions. But there is a lot of more than only the action by using custom actions. You can use the permission set, in order to define which permission level a user must have to click or see the custom action. It does not support User or Groups but maybe next Version or next decade.

The other useful function is about the enable/disable cause of some code. For example, if your custom action should only be enabled if the user selected more than one or two items.

If the user selects a folder (Test2 is a folder in a tasklist) the custom action is disabled. But if the selection is a normal item then it will be enabled.

Let’s have a look at the code for the function:

[sourcecode language=”javascript”]

function makeAvailable() {
var enable = true;
var context = SP.ClientContext.get_current();
var selection = SP.ListOperation.Selection.getSelectedItems(context);

//Check if at minimum one is selected
if (CountDictionary(selection) > 0)
this.enable = true;
else
return false;
//Check if a folder is selected
var item;
for (item in selection) {
if (selection[item].fsObjType != 0)
enable = false;
}
return enable;
}

[/sourcecode]

At first we check if the user selected at least one item:
“if (CountDictionary(selection) > 0)”

Then we iterate through each selected item and check the value of .fsObjType. If the value is 0 then it is a folder. So the key is this line:
if (selection[item].fsObjType != 0)

This function can be embedded into your custom action as you might know by using the commanduiHandler section:

[sourcecode language=”csharp”]



[/sourcecode]

That’s it.

..:: I LIKE SHAREPOINT ::..

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

About Karsten Schneider 312 Articles
Consultant for Microsoft 365 Applications with a strong focus in Teams, SharePoint Online, OneDrive for Business as well as PowerPlatform with PowerApps, Flow and PowerBI. I provide Workshops for Governance & Security in Office 365 and Development of Solutions in the area of Collaboration and Teamwork based on Microsoft 365 and Azure Cloud Solutions. In his free time he tries to collect tipps and worthy experience in this blog.

Leave a Reply

Your email address will not be published.


*