Monday, July 07, 2014

How to Enable/Disable Ribbon Buttons by User using EnabledScript and REST Code

Hi all, 

This article is again an oldie but a nice one to remember about the Ribbons (since SP 2010) and how we can manage their visibility. I am not saying nothing new because there are a lot of content about this topic on internet that can be used in your solution also this one (there are really good stuff and yes i am lazy...) to include in my business case "Disable Ribbons to specific user using EnabledScript".


This example creates a Custom Ribbon that visibility should be define to a specific user.
The solution uses EnabledScript with REST calls to indentify current user and enable/disable the Ribbon. 

Since i use RibbonManager App interface to create/manage/deletion/recycleBin of Custom Actions and complex XML structure, this give more focus to developers response the real business needs with their code.
There the RibbonManager App is helper to reduce time in a very boring task of XML configuration (some XML take hours...) and if you can do it in 5 minutes your life can be more easy and you become more efficient....   :)
Another thing from the RibbonManager App SharePoint App, don't need Farm or Sandbox deployments, uses the SharePoint App Model to work with Office365 and SharePoint On-premise 2013, this can take out the time to create IQ documents and discussions with IT Managers for approval of your Solution. :)

Here is the final output:





About code related, 
There is a lot of documentation talking about this topic EnabledScript in Ribbons you should read about to support your developmenthere are a small list of articles :
CommandUIHandler Element
http://msdn.microsoft.com/en-us/library/office/ff458385(v=office.15).aspx
Asynchronously Checking if a Command is Available in the SharePoint Ribbon
http://www.andrewconnell.com/blog/Asynchronously-Checking-if-a-Command-is-Available-in-the-SharePoint-Ribbon
Code example for EnabledScript element in Ribbon XML
http://henrikfromsweden.blogspot.ch/2010/11/code-example-for-enabledscript-elemnt.html
Using RefreshCommandUI with the Server Ribbon (Dallas Tester, Fred Mameri)
http://blogs.msdn.com/b/sharepointdev/archive/2011/02/17/using-refreshcommandui-with-the-server-ribbon.aspx
Customizing the ribbon (part 1) – creating tabs, groups and controls
http://www.sharepointnutsandbolts.com/2010/01/customizing-ribbon-part-1-creating-tabs.html
Sample code: Asynchronously checking if a Ribbon command is available
http://blog.mastykarz.nl/sample-code-asynchronously-checking-ribbon-command/
Enable or disable custom ribbon button in SharePoint 2010
http://tomaszrabinski.pl/wordpress/2012/02/25/enable-or-disable-custom-ribbon-button-in-sharepoint-2010/

About the creation of the Custom Ribbon structure was associated the Registration type Document Libraries ID -101 and associate to new Ribbon the Location "Ribbon.Library.Actions"



When the new Ribbon is created the validation Script should added in the field "Enabled" associated to EnabledScript in the XML structure.



Validation Code to Enable/Disable Ribbon Button

This code has to be included in the EnabledScript option of the XML Sctructure associated with the Ribbon described in point "Add Ribbon Custom Action and ScripLink to support solution in SharePoint Site" .

****************Code Example****************
javascript:var name;
//Get Current User Name and send welcome message
getCurrentUser(function(userData){
alert("Welcome back Mr. "+userData);
name=userData;
});
//Get User Name REST call
function getCurrentUser(success)
{$.ajax({    
url:  _spPageContextInfo.webServerRelativeUrl + ((_spPageContextInfo.webServerRelativeUrl.indexOf('/', _spPageContextInfo.webServerRelativeUrl.length - 1) !== -1) ? '' : '/') + "_api/web/currentUser?$select=Title",
method: "GET",
    headers: { "Accept": "application/json; odata=verbose" },    
success: function (data) {success(data.d.Title);},  
async: false,
error: function (error){alert(error.status);}   
});
}
//This validation returns true ="Enable Option" or false="Disable Option"
if (name=='Andre Lage')
{true;}else{false;}


User Validated

Access the Library where the Custom Ribbon was created and should appear Enable.




User not Validated

If you change in the code the following line
"if (name=='Andre Lage')" to "if (name!='Andre Lage')
the next time you access the Library with Custom Ribbon should appear Disable.



Add Ribbon Custom Action and ScripLink to support solution in SharePoint Site

I use the RibbonManager App, to include the Scriplinks and Ribbons in the Host Web Site to became accessible in SharePoint Site (is faster and more efficient to create the interface and Developers should only focus in the business code).
If you like to use Visual Studio and Declarative code you can use the following XML.

For the Custom Ribbon you can include the Custom Ribbon Action with the following XML Structure:

<CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">
<CommandUIDefinitions>
<CommandUIDefinition Location="Ribbon.Library.Actions.Controls._children">
<Button Id="My Custom Button" LabelText="My Custom Button" Image32by32="/_layouts/15/1033/images/formatmap32x32.png?rev=33" Image32by32Left="-168" Image32by32Top="-68" ToolTipTitle="My Custom Button" Command="My Custom Button.Command" TemplateAlias="o1" />
</CommandUIDefinition>
</CommandUIDefinitions>
<CommandUIHandlers>
<CommandUIHandler Command="My Custom Button.Command" CommandAction="<Add action Code here>" EnabledScript="<Add validation Code here>" />
</CommandUIHandlers>
</CommandUIExtension>

For the Jquery you can use the following XML Structure:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="Global Jquery" Title="Global Jquery" Description="" Location="ScriptLink"ScriptSrc="~Site/SiteAssets/jquery-1.10.2.min.js" Sequence="1" > </CustomAction>
</Elements>

Here a video with all the steps to create this Action:




Support Links:
CommandUIHandler Element
http://msdn.microsoft.com/en-us/library/office/ff458385(v=office.15).aspx
Asynchronously Checking if a Command is Available in the SharePoint Ribbon

http://www.andrewconnell.com/blog/Asynchronously-Checking-if-a-Command-is-Available-in-the-SharePoint-Ribbon
Code example for EnabledScript element in Ribbon XML
http://henrikfromsweden.blogspot.ch/2010/11/code-example-for-enabledscript-elemnt.html
Using RefreshCommandUI with the Server Ribbon (Dallas Tester, Fred Mameri)
http://blogs.msdn.com/b/sharepointdev/archive/2011/02/17/using-refreshcommandui-with-the-server-ribbon.aspx
Customizing the ribbon (part 1) – creating tabs, groups and controls
http://www.sharepointnutsandbolts.com/2010/01/customizing-ribbon-part-1-creating-tabs.html
Sample code: Asynchronously checking if a Ribbon command is available
http://blog.mastykarz.nl/sample-code-asynchronously-checking-ribbon-command/
Enable or disable custom ribbon button in SharePoint 2010
http://tomaszrabinski.pl/wordpress/2012/02/25/enable-or-disable-custom-ribbon-button-in-sharepoint-2010/

Hope you enjoy the Article, 
Kind regards,
André Lage

Post a Comment