
Using custom action you can create your own ribbon elements. In a post I have published a while ago I showed how to add a button to the ribbon to generate a QR-Code. But what if you want your button only to be enabled under certain circumstances. For example; if the user has sufficient privileges.
You can specify a JavaScript function in the EnabledScript element within the custom action which will be called by SharePoint if the availability of the button should be checked.
<CustomAction Id="AlainDeKlerk.SP2010.DocumentIdLink" Location="CommandUI.Ribbon" RegistrationId="101" RegistrationType="List"> <CommandUIExtension> <CommandUIDefinitions> <CommandUIDefinition Location="Ribbon.Documents.Share.Controls._children"> <Button Id="AlainDeKlerk.SP2010.DocumentIdLink" Command="CreateDocumentIdLink" Image16by16="/_layouts/images/URL16.GIF" Image32by32="/_layouts/images/URL32.GIF" LabelText="Get Document ID Link" TemplateAlias="o2" /> </CommandUIDefinition> </CommandUIDefinitions> <CommandUIHandlers> <CommandUIHandler Command="CreateDocumentIdLink" CommandAction="javascript:AlainDeKlerk_CreateDocumentIdLink();" EnabledScript="javascript:AlainDeKlerk_IsAvailable_CreateDocumentIdLink();" /> </CommandUIHandlers> </CommandUIExtension> </CustomAction>
As an example; I will demonstrate a EnabledScript that will enable the button of 4 conditions are met:
- Exactly one item is selected
- The selected item is not a folder
- The current user has enough permissions to edit the selected item
- The selected item does not has the word ‘confidential’ in its name
Conditions (1) and (2) are quite easy to check, however conditions (3) and (4) are a bit more difficult but can be accomplished by querying SharePoint using the Client Side Object Model. The JavaScript below can be used to enable or disable a ribbon button based on the described conditions:
var __document_id_permissions = new Array() function AlainDeKlerk_IsAvailable_CreateDocumentIdLink() { var items = SP.ListOperation.Selection.getSelectedItems(); if (items.length == 1) { if (items[0].fsObjType == '0') { return CheckListItem(items[0]); } } } function CheckListItem(item) { if (__document_id_permissions[item.id] === undefined) { var context = SP.ClientContext.get_current(); var list = context.get_web().get_lists().getById(SP.ListOperation.Selection.getSelectedList()); this.listItem = list.getItemById(item.id); context.load(this.listItem, 'FileRef', 'EffectiveBasePermissions', "ID"); context.executeQueryAsync(Function.createDelegate(this, this.onCheckListItemSucceeded), Function.createDelegate(this, this.onCheckListItemSucceededFailed)); return false; } return __document_id_permissions[item.id]; } function onCheckListItemSucceeded(sender, args) { var __current_user_has_permissions = this.listItem.get_effectiveBasePermissions().has(SP.PermissionKind.editListItems); var __document_is_confidential = this.listItem.get_item('FileRef').indexOf('confidential') >= 0; var __itemId = this.listItem.get_item('ID') __document_id_permissions[__itemId] = __current_user_has_permissions && !__document_is_confidential if (__document_id_permissions[__itemId]) { RefreshCommandUI(); } } function onCheckListItemSucceededFailed(sender, args) { alert('request failed ' + args.get_message() + 'n' + args.get_stackTrace()); }
This solution is just what I need.
My question is where to implement the “”? You used a visual web part or inserted the code in Master Page?
Thanks