How to Hide Buttons Based on User Security Role in Dynamics 365 Sales

By Herb Johnson | August 31, 2022

Recently, we received a request from a customer who wanted to hide the Activate and Deactivate buttons on the Contact ribbon based on the user security role in Dynamics 365 Sales.

In this case, they wanted to hide them for users with the Salesperson security role.

Hide Buttons Based on User Security Role in Dynamics 365 Sales Customer Request

There is a solution for this, and in this blog post, we will detail how you can take advantage of Ribbon Workbench and JavaScript to achieve it. The solution works for On-Prem (v8) and Online Dynamics 365 (v9.) The solution for both is very similar, with the only difference being one line of JavaScript, which we will highlight below.

To begin, we will do the following:

  • 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.

Hide Buttons Based on User Security Role in Dynamics 365 Sales Role Guid

1 - Find Salesperson security role Guid - Open Security Role and copy the Guid shown below

  • Do not include the prefix, '%7b' or the end, '%7d'

2 - 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: 

  • //
    // Hide the Activate button and the Deactivate button when user has Salesperson security role
    //
    function hideActivateAndDeactivateRibbonButtons() {
    //
    // default return value to true. Show buttons
    //
    var value = true;
    //
    // Set variable with SalesPerson security role GUID
    //
    var constSalesPersonGuid = "74E3A3A3-E6F3-4B97-A045-A557B1660293";
    //
    // Get the Current User's Security Roles
    //
    // D365 v8
    // var userRoles = Xrm.Page.context.getUserRoles();
    //
    // D365 v9
    var userRoles = Xrm.Utility.getGlobalContext().userSettings.securityRoles;
    //
    // loop through roles and look for SalesPerson guid
    //
    for (var i = 0; i < userRoles.length; i++) {
    if (userRoles.toUpperCase() == constSalesPersonGuid.toUpperCase()) {
    //
    // Hide Activate button and the Deactivate button
    //
    value = false;
    break;
    }
    }
    return value;
    }

3 - Create Enable Rule in Ribbon Workbench

A -  Create a solution with the Contact table

B - Open solution with Ribbon Workbench

C - Right-click on Activate button

C1 - Select Customize Command

Hide Buttons Based on User Security Role in Dynamics 365 Sales Customize Command

C2 - Click + next to Enable Rules

Hide Buttons Based on User Security Role in Dynamics 365 Sales Enable Rules

C3 - Click Add Step

Hide Buttons Based on User Security Role in Dynamics 365 Sales Add Step

C4 - Click CustomRule

Hide Buttons Based on User Security Role in Dynamics 365 Sales CustomRule

C5 - Enter the function name from the Contact JavaScript Library

  • 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:

Hide Buttons Based on User Security Role in Dynamics 365 Sales Enable Custom Rule

D - Click on the Command and then click Add Enable Rule

Hide Buttons Based on User Security Role in Dynamics 365 Sales Add Enable Rule

E - Click on the Enable Rule you created previously 

Hide Buttons Based on User Security Role in Dynamics 365 Sales Enable Rule Access

4 - For the Deactivate Button

A - Right-click on the Deactivate Button and Select Customize Command

B - Since the enable Rule already exists, we don't need to perform that step

C - Click on Command, and then click Add Enable Rule

Hide Buttons Based on User Security Role in Dynamics 365 Sales Deactivate

D - Click on the Enable Rule you previously created

Hide Buttons Based on User Security Role in Dynamics 365 Sales Add New Enable Rule

5 - Click Publish in the Ribbon Workbench

Hide Buttons Based on User Security Role in Dynamics 365 Sales Publish

6 - 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. 

Hide Buttons Based on User Security Role in Dynamics 365 Sales Test

B - Add the Salesperson security role to your user

Hide Buttons Based on User Security Role in Dynamics 365 Sales Assign Security Role to User

C - Go back to the Contact and do a hard refresh of the browsers tab and there should be no Deactivate button. 

Hide Buttons Based on User Security Role in Dynamics 365 Sales Test Deactivate Button

D - Test the activate button by: 

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.

As you can see, it doesn't take much to hide ribbon buttons. It's a pretty straightforward process and a technique you can apply to any ribbon buttons on any form. If you are going to hide buttons across multiple forms, you only need the JavaScript function in one web resource.

Questions?

Hopefully, this blog gave you clear steps on how to utilize this solution! If you have any questions, please reach out to us and our experts will be happy to assist you.

Related Posts


Under the terms of this license, you are authorized to share and redistribute the content across various mediums, subject to adherence to the specified conditions: you must provide proper attribution to Stoneridge as the original creator in a manner that does not imply their endorsement of your use, the material is to be utilized solely for non-commercial purposes, and alterations, modifications, or derivative works based on the original material are strictly prohibited.

Responsibility rests with the licensee to ensure that their use of the material does not violate any other rights.

Start the Conversation

It’s our mission to help clients win. We’d love to talk to you about the right business solutions to help you achieve your goals.

Subscribe To Our Blog

Sign up to get periodic updates on the latest posts.

Thank you for subscribing!