Step By Step Process to hide/show Ribbon button based on security role using javascript
Hide the OOB Activate and Deactivate ribbon buttons on the D365 Form/Views based on the user security role in Dynamics 365.
On the form Activate/Deactivate Ribbon Button:
- Create a JavaScript function that returns true or false based on whether the user has the Salesperson security role. You will see the JavaScript below:
- False will hide the buttons
- True will show the buttons
- Put this function in the Contact JavaScript Library web resources. This makes it available in the ribbon and determines if the buttons should be visible.
- Edit the buttons in the Ribbon Workbench to use the new function.
Now, let's take a look at the steps to make this happen.
1. Create JavaScript
A - Open or create a web resource to hold the JavaScript function.B - For our purposes, the web resource is stn_ContactJavaScriptLibrary
C - Paste the following JavaScript into the web resource:
var value = true;
var constSalesPersonGuid = "74E3A3A3-E6F3-4B97-A045-A557B1660293";
var userRoles = Xrm.Utility.getGlobalContext().userSettings.securityRoles;
for (var i = 0; i < userRoles.length; i++) {
if (userRoles[i].toUpperCase() == constSalesPersonGuid .toUpperCase()) {
value = false;
break;
}
}
return value;
},
2 - Create Enable Rule in Ribbon Workbench
A - Create a solution with the Contact tableB - Open solution with Ribbon Workbench
C - Right-click on Activate button
C1 - Select Customize Command
In this case, it is: hideActivateAndDeactivateRibbonButtons
C6 - Click the magnifying glass and select the Contact JavaScript Library
C7 - It should look like this when you are done with this step:
D - Click on the Command and then click Add Enable Rule
E - Click on the Enable Rule you created previously
3 - For the Deactivate Button
A - Right-click on the Deactivate Button and Select Customize CommandB - Since the enable Rule already exists, we don't need to perform that step
C - Click on Command, and then click Add Enable Rule
5 - Test it out!
You are now ready to give this solution a test before going live. Just follow these steps:
A - Look at the Contact Ribbon for a user who does not have the Salesperson security role. You will notice the Deactivate button.
C - Go back to the Contact and do a hard refresh of the browsers tab and there should be no Deactivate button.
D1 - Removing the Salesperson role from the user
D2 - Doing a hard Refresh of the Contact tab
D3 - Check to see if the Deactivate button is visible on the Contact
D4 - Click the Deactivate button
D5 - Now you should see the Activate button
D6 - Add the Salesperson role to the user
D7 - Do another hard refresh of the Contact tab
D8 - And now the Activate button is no longer visible.
Note: To hide Activate/Deactivate button on View - Add the same enable rule as how it's done for form.
No comments:
Post a Comment