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:



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;
}


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:



<commanduihandlers>
<commanduihandler Command="MoveItems"
CommandAction="javascript:openDialog();"
EnabledScript="javascript:makeAvailable();"></commanduihandler>
</commanduihandlers>


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.

Karsten Pohnke About Karsten Pohnke
He is Consultant for SharePoint Solutions for collaboration, communication and business processes. He provides his customers applications based on standard features as well as development or combining the power of several microsoft tools like Dynamics CRM. In his free time he tries to collect tipps and worthy experience in this blog.

2 comments on “SharePoint 2010 Disable custom action button if selected item is a folder

  1. rishkarthik

    Hi, How do we know current user is a site admin or not in the ECMA script /Javascript

    -karthik

Submit comment

Allowed HTML tags: <a href="http://google.com">google</a> <strong>bold</strong> <em>emphasized</em> <code>code</code> <blockquote>
quote
</blockquote>

Please fill in the captcha: * Time limit is exhausted. Please reload CAPTCHA.