Show custom ribbon button based on Security Role of the logged in User in Dynamics 365 | Ribbon Workbench in XrmToolbox

In ribbon button customization, it is a common scenario to show the button only to a certain set of users who have a certain security role.

Security Role Scenario

  1. Let’s assume Subscription Manager is a security role in your Dynamics 365.

  2. And the Ribbon button will only be visible to the Users who have been assigned this Security Role.

  3. If they have this Role, they’ll be able to see the button as below

  4. And, if the Role is not assigned, the logged In user won’t be able to see the Button.

  5. See below that in this case, the button will not show up.


JavaScript Code to check assigned Security Roles to the logged in User

  1. Since we are going to use a CustomRule further in the Ribbon Workbench to pick a true or false value based on whether the logged in user has a Security Role or not, let’s write a quick JavaScript function to provision the same.

Tip: Make sure you now pass the PrimaryControl (context) to any JS functions and avoid using Xrm.Page since the same has been deprecated.

// JavaScript source code
contactFormCustomization = {
    checkSubscriptionAccess: function (context) {
        "use strict";
        debugger;
        var currentUserRoles = context._globalContext._userSettings.securityRoles;
        var roleId = "BA69EA1F-A76E-EB11-A812-000D3A1948AB"; // Subscription Manager role
        roleId = roleId.toLowerCase();
        // Get all the roles of the Logged in User.
        
        for (var i = 0; i < currentUserRoles.length; i++) {
            var userRoleId = currentUserRoles[i];
            if (userRoleId == roleId) {
                // Return true if the Role matches
                return true;
            }
        }
        return false;
    }
    
};

Refer this post which discusses a simple JavaScript function to use in order to check if the logged in User has a certain security role or not – https://medium.com/capgemini-microsoft-team/dynamics-365-v9-verify-logged-in-user-security-role-using-typescript-2de52f2ef04e

Explanation

  1. Hard-code the GUID of the Security Role which you are looking to check.
  2. Then read all the Security Roles assigned to the user.
  3. Once the Security Roles are found in the logged in User’s Security Role, return true. Else, return false.

Ribbon Button – Enable Rule

Let’s see how the button customization will look like in XrmToolBox’s Ribbon Customization –

  1. In Ribbon Workbench, you need to add a CustomRule under Enable Rules for the Ribbon button.

  2. Then, it asks for the JavaScript function (mention the function which returns a simple true or false based on above steps). and then mention the library –
    Also, pass the context PrimaryControl and using the same, pick the Security Roles of the logged in user as mentioned in the JS code explanation above.
    I’m naming my Enable Rule as SecurityRoleCheck.

    Now, the CustomRule I’ve applied will call the JS function and is expected to receive either a true or a false based on the code. If false – the button will not be enabled, if true – the button will be enabled.


  3. Now, make sure you add this Enable Rule to the Command (which is in-turn attached to the Ribbon Button itself)

Hope this was helpful. Here are some more XrmTool / Ribbon Button customization related posts you might find helpful –

  1. Ribbon button visibility based on a field value in Dynamics 365 | Ribbon Workbench
  2. Get GUID of the current View in Dynamics 365 CRM JS from ribbon button | Ribbon Workbench
  3. Pass Execution Context to JS Script function as a parameter from a Ribbon button in Dynamics 365 | Ribbon Workbench
  4. Pass selected rows’ GUIDs to ribbon button in D365 | Ribbon Workbench
  5. Debug Ribbon button customization using Command Checker in Dynamics 365 CE Unified Interface
  6. Show Ribbon button only on record selection in Dynamics CRM
  7. Hide Custom Ribbon Button [Easy Way] – Ribbon Workbench
  8. Enable Flow button on D365 Ribbon
  9. [SOLVED] Navigating URL from Ribbon’s custom button in Dynamics for Phones app
  10. Fix Ribbon icons on the Unified Interface in D365 CE
  11. Connecting XrmToolBox to an MFA enabled Dynamics 365 environment | Azure AD
  12. Find deprecated JS code used in your Dynamics 365 environment | Dynamics 365 v9 JS Validator tool | XrmToolBox

Thank you!!

Advertisement

Ribbon button visibility based on a field value in Dynamics 365 | Ribbon Workbench

Most times, requirement is as simple as being able to show or hide a ribbon button based on a value of the field on the record.

Scenario

Let’s consider this scenario, a button on Lead entity called as “Evaluate” exists. It should be only visible if the Lead Source (OptionSet) is set to Partner.


And the button should be hidden if the condition is not satisfied.

Enable Rule based on Value

Now, let’s see how we achieve this –

  1. In Ribbon Workbench, in your Command which is tied to the button, you need to add a Display Rule.

  2. In the Display Rule, you’ll need to select ValueRule.

  3. In the value Rule, you’ll need to specify the field name and the value to consider.
    So here, the field I want to consider is “leadsourcecode” i.e. “Lead Source” field on the Lead entity and 4 is the value of the Partner option.


  4. and finally make sure your Display Rule is correctly tied to the Command on the button (And yes, the Command should also be tied to the Button itself.)

  5. And finally Publish your changes.

Execution

Now, when the Lead has value as Partner, the button will be visible

And when the value is something else, it will be hidden once the record is saved.

Note: In this case if your condition is based on multiple values, consider adding more Display Rules and patch them together using OrRule as one Rule will cater to only 1 value.

Hope this helps!

Here are some more Ribbon Workbench related posts you might want to check out –

  1. Get GUID of the current View in Dynamics 365 CRM JS from ribbon button | Ribbon Workbench
  2. Pass Execution Context to JS Script function as a parameter from a Ribbon button in Dynamics 365 | Ribbon Workbench
  3. Pass selected rows’ GUIDs to ribbon button in D365 | Ribbon Workbench
  4. Debug Ribbon button customization using Command Checker in Dynamics 365 CE Unified Interface
  5. Show Ribbon button only on record selection in Dynamics CRM
  6. Hide Custom Ribbon Button [Easy Way] – Ribbon Workbench
  7. Enable Flow button on D365 Ribbon
  8. [SOLVED] Navigating URL from Ribbon’s custom button in Dynamics for Phones app
  9. Create a New Record button for Activity Type entity using Ribbon Workbench: D365
  10. D365 Ribbon Button shortcut to open a Document in SharePoint Online

Thank you!!

Get GUID of the current View in Dynamics 365 CRM JS from ribbon button | Ribbon Workbench

You already know the GUID of a view, nothing surprising. But, you want to get what current view is active. Because at times, you want to compare and return values to a CustomRule based on what view is being set currently!

Let’s take a look!

Passing Parameter to JS function in Ribbon Workbench

Let’s assume the below scenario –

  1. You have a Ribbon button on the Account entity. And it looks like below.

  2. So here’s what you pass to the function that this button will call. No matter where that function is being called i.e. on Press of the button in Command or as a Custom Rule in an Enable Rule. You can pass this parameter called as PrimaryControl (If you are calling your function on selection of a record, you can even use SelectedControl and continue as is)

Reading GUID of the current view in Browser’s Dev Console

Now, let’s say you have a button on the selection of a Row or even on press of the button itself and it calls the function to which you’ve already passed PrimaryControl, here’s how you read it –

  1. Notice that the argument is passed, set a debugger; so that you don’t have to manually set breakpoints.

  2. Now, go to Console, and check this variable in which PrimaryControl is being passed. You’ll notice that _getCurrentView() is available for you

  3. Simply, select it in the function and press enter, you’ll see details inside.

  4. And you can simply read the id like you read an attribute.

Hope this helps!!

Here are some more Ribbon Workbench / JS / customization related posts you might want to look at –

  1. Pass Execution Context to JS Script function as a parameter from a Ribbon button in Dynamics 365 | Ribbon Workbench
  2. Pass selected rows’ GUIDs to ribbon button in D365 | Ribbon Workbench
  3. Debug Ribbon button customization using Command Checker in Dynamics 365 CE Unified Interface
  4. Show Ribbon button only on record selection in Dynamics CRM
  5. Hide Custom Ribbon Button [Easy Way] – Ribbon Workbench
  6. Enable Flow button on D365 Ribbon
  7. [SOLVED] Navigating URL from Ribbon’s custom button in Dynamics for Phones app
  8. Fix Ribbon icons on the Unified Interface in D365 CE
  9. Create a New Record button for Activity Type entity using Ribbon Workbench: D365
  10. D365 Ribbon Button shortcut to open a Document in SharePoint Online

Thank you!!

Pass Execution Context to JS Script function as a parameter from a Ribbon button in Dynamics 365 | Ribbon Workbench

One of the most common searches around customizing ribbon buttons is to send the Execution Context / Form Context as a parameter to the JavaScript function your Ribbon button is set to call.

Pass Form Context to the JS in Ribbon

Let’s see how you can pass form context as a parameter to the JS method on from your Ribbon button

  1. Let’s say you have a ribbon on the Opportunity that does something based on budget, so I’ve called it Budget.

  2. In the Ribbon Workbench, the button is calling the command which has a JS Function tied to it. Add a CRM Parameter to the JavaScript Action

  3. Once you select CRM Parameter, you’ll need to pass on the Primary Control as shown below

  4. Then, it’ll look like this on your JS Function

Accessing Form Context

Let’s say you have opened the Dev Tools on your browser by pressing F12, then click the button (assuming you either have set a breakpoint inside the function or using debugger; to stop at a point inside the function that button calls). And then you click the button


Once you debug, you’ll see this when you hit the debugger when you function is called.
The debugger will be hit

And if you check in the Console, you’ll see the complete context passed to the function

Hope this helps!

Here are some Ribbon Workbench related posts you might want to check –

  1. Pass selected rows’ GUIDs to ribbon button in D365 | Ribbon Workbench
  2. Debug Ribbon button customization using Command Checker in Dynamics 365 CE Unified Interface
  3. Show Ribbon button only on record selection in Dynamics CRM
  4. Hide Custom Ribbon Button [Easy Way] – Ribbon Workbench
  5. Enable Flow button on D365 Ribbon
  6. [SOLVED] Navigating URL from Ribbon’s custom button in Dynamics for Phones app
  7. Fix Ribbon icons on the Unified Interface in D365 CE
  8. D365 Ribbon Button shortcut to open a Document in SharePoint Online

Thank you!

Pass selected rows’ GUIDs to ribbon button in D365 | Ribbon Workbench

Whenever your Ribbon button customization involves doing something to selected records, here’s the most common application that you need to take action on the selected record’s GUIDs.

Here’s how –

CRM Parameter

In Ribbon Workbench, let’s say you have a button command which is attached to a button and it calls a method ‘process’ which looks like this (it will be your method in your case)

  1. In Commands, Custom JavaScript Action will have a CRM Parameter called as SelectedControlSelectedItemIds [pretty self explanatory 😊]
    undefined


    And it’ll pass data of the selected GUIDs in my data variable passed to the method.
    undefined

Read the GUIDs

Here’s my sample code where I intend to read it from the function I used to call when user clicks on the ribbon button

  1. Now, let’s say I select a few records on the Main Sub-grid where I’ve put my button on.
    undefined

  2. Now, on clicking it, it’ll pass the three selected records’ GUIDs to the method that the button is calling. You can read it from the data parameter
    undefined
  3. You can further read from this array and use it to perform your operations.

Hope this was easy. Here are some more Ribbon Customization related posts you might want to look at –

  1. Debug Ribbon button customization using Command Checker in Dynamics 365 CE Unified Interface
  2. Show Ribbon button only on record selection in Dynamics CRM
  3. Hide Custom Ribbon Button [Easy Way] – Ribbon Workbench
  4. [SOLVED] Navigating URL from Ribbon’s custom button in Dynamics for Phones app
  5. Fix Ribbon icons on the Unified Interface in D365 CE
  6. D365 Ribbon Button shortcut to open a Document in SharePoint Online
  7. Pass data to HTML Web Resource using browser’s sessionStorage in Dynamics 365 CE

Thank you!!

Show Ribbon button only on record selection in Dynamics CRM

One of the most common asks is to show ribbon buttons to only be shown when at least 1 record in Dynamics 365 ribbon button are selected.
buttonShow

Here’s how you can do it –

Enable Rule in Ribbon Workbench

This can be achieved using Enable Rule for the button you wish to show on selection using Ribbon Workbench.

Let’s say your button ‘Promote’ (in this case) is on the Main View of the Account entity. You can add the following Enable Rule to the command –
enableRuleBound

And in the actual Enable Rule, you can select the SelectionCountRule to make this happen. You can set the minimum value to 1 so that it appears when at least 1 record is selected.
acutalEnableRule

Result

Now, if you see – By default, the button is hidden –
buttonHidden

Now, as you can see, only when at least 1 record from the view is selected, the button is visible.
buttonShow

In case you are looking to hide a custom button, check this post – Hide Custom Ribbon Button [Easy Way] – Ribbon Workbench

Hope this helps!!