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 ::..
Hi, How do we know current user is a site admin or not in the ECMA script /Javascript
-karthik
Hi, maybe you could look at these links which could help you
– http://blogs.msdn.com/b/edhild/archive/2008/01/16/how-to-add-security-trimming-info-to-custom-actions-in-sharepoint.aspx
– http://www.alaindeklerk.com/conditionally-enabledisable-ribbon-buttons-based-on-user-permissions/