Hide and Show Ribbon Button Based on User Security Role in Dynamics 365 v9.x

Hello Everyone, Today i would like to share my knowledge regarding show and hide ribbon button based on the user Security roles.

I have a custom entity name as “account1”. I have to create two button (Submit, Complete) using ribbon workbench.We have two type of user one is Salesperson, second one Sales Manager. So in this blog i am going to cover some of the points as given below.

1.If user security role is Salesperson, show “Submit” button and hide the “Complete” button.

2.If user security role is Sales Manager, show “Complete” button and hide the “Submit” button.

3.After creation of the record user will able to see the buttons(Submit or Complete).

Lets start.

I created a solution named as AccounRibbon and added my entity account1. Import the latest ribbon workbench into your organisation if already not done. Once it is imported successfully, open the the ribbon workbench and select your solution, i am selecting the  Account Ribbon.

1

Once your solution loaded successfully,select the entity and add the buttons in to main form by drag and drop. Give the proper names to buttons.

2

once button added we need to add the command and the enable rules for both the buttons.

3

Lets first talk about the Enable Rules.In Simple words, Enable rule are used as trigger when our buttons will enable for form.So first we need to check both the button will available only when the form type is update, for that we are going to use form state rule.

Click on the Submit Enable rule as shown in above screenshot, on the right panel , click on add step and choose FormStateRule.

4
5

Use existing because we want show this button after the record is created. Set invert rule as true, it means if the form is not “Existing” type then button will hide automatically. Same step follow for the Completed Enable rule. once both is done , now we have to write a CustomRule that will check the user security role and based on the user security role we will return true or false from the function.

NOTE:Please don’t focus on the other two ValueRule  i am doing some other calculation as well that is not part of the this blog.

create a web resource and add this JS in your web resource.

function getCurrentUserSecurityRolesIfsalesperson()
{
    debugger;
    var value=false;
    var userRoles=Xrm.Utility.getGlobalContext().userSettings;
    if(Object.keys(userRoles.roles._collection).length>0)
    {
        for ( var rolidcollection in userRoles.roles._collection)
        {
           var currentUserRoles= Xrm.Utility.getGlobalContext().userSettings.roles._collection[rolidcollection].name;    
           if(currentUserRoles.toLowerCase()=="salesperson")
           {
               value=true;
               break;
           }
        }
    }
    return value; 
}

After adding this JS publish the web resource and go back to the ribbon workbench.

Add new Step in Enable rule and select custom rule. Specify the property as shown in image.

This is completed for one button submit repeat the same process for complete button do necessary changes.e.g match with sales manager role like i am matching with salesperson.

Now you have both the enable rule done. Add both the enable rule to respected command as shown in below image.

Once it is added. you can specify the action-> what you want to perform after click on button. as given in the snap. Perform the same steps for the Complete command as well. Now go to the button tab select the button and add the command respected to your button.

Once the command added , perform same operation for the Submit button.then click on publish.

Once you follow all the steps you will easily achieve what i shared in blog.

Thanks for seeing. Hope you like it. If you have any query please use comment box.

Advertisement

Get Logged In User Security Roles in Dynamics 365 v9.1

Hello , Hope Everyone doing very well. i want to share something which a created for you guys. i was working on a requirement where i need to get logged in user security role.Most of you will already know but i discover a new method,a very short code without using any web API . This will help you to save your time to writ a big code.

function getCurrentUserSecurityRoles(executionContext)
{
	//Returns information about the current user settings.
    var userRoles=Xrm.Utility.getGlobalContext().userSettings;	
	//it will give you count of security a user is having
    if(Object.keys(userRoles.roles._collection).length>0)				
    {
		//userRoles.roles._collection will give you the index of the Current user security role , it contains roleid , and name of the security role
        for ( var rolidcollection in userRoles.roles._collection)		
        {
			//Once you get the index of the security role from where you can retrive id, name of the Security Role
           var currentUserRoles= Xrm.Utility.getGlobalContext().userSettings.roles._collection[rolidcollection].name;    
           console.log(currentUserRoles);
        }
    }
}

If you have any question feel free to ask :).