Tuesday, July 28, 2015

How to Enable/Disable a custom button for specific roles in Dynamics CRM??



I just came across with an urgent requirement to add a custom button in to main ribbon and then enable it if user has CSR Manager Role and disable in other case. It can be done through many ways as you can find multiple blogs posts written on it. But I found a simplest way to do it.
Here it is:
  1. Find guid of CSR Manager Role using the following code:

    function CheckUserRole(){
    //get guid of all assigned roles
    var currentUserRoles = Xrm.Page.context.getUserRoles();
    for (var i = 0; i < currentUserRoles.length; i++) {
            
    var userRoleId = currentUserRoles[i];
             //alert to each role
           
    alert(userRoleId);
    }   
    return false;
    }
  2.  In the following code, I’m checking if array contains the guid of a CSR Manager Role

    function ifCSRManager() {
    //get  guids of the roles assigned to user
    var currentUserRoles = Xrm.Page.context.getUserRoles();
    //check if it contains the guid of CSR Manager
    if (currentUserRoles.indexOf("1117f744-a5bd-e411-8c0a-005056b04acd") > -1)
     
    {    return true;}
    return false;
    }
Now, in ribbon workbench just add custom javascript role in enable rule and then tie it with command of the button.

Step # 1: Add new Enable Rule


Step # 2: Right click on the rule and then Edit Rule 

Step # 3: Select Custom Javascript Rule and click OK 

Step #4: Give Function Name and Library that contains function and click OK 
Step # 5: Add this rule to the command tied with your custom button and publish solution.




No comments:

Post a Comment