While doing any development with Dynamics 365 CRM, there might be a requirement of handling a few things like form attributes based on the security role. In many projects or applications of Dynamics 365 CRM where developers have to handle a few fields or tabs of CRM by showing/hiding the same to Dynamics 365 users based on their security role. To do so, developer retrieves the security role of logged-in user and checks for the specific security role to show/hide fields or any other controls of CRM.
Now, to retrieve the user’s security role we were using Microsoft client API as below:
var userSettings = Xrm.Utility.getGlobalContext().userSettings
Using above API we get the logged-in user’s details like User Id, Date format, Security roles, etc.
Further, to get the security roles we used userSettings.securityRoles
So, using this we get the list of all role Id of security roles assigned to the logged-in user. However, we could not get the security role names. So, we need to write another code to retrieve all the security roles from CRM and then, we need to write the logic to get the security role name by using the role Id.
However, now we do not have to write extra code to get security role name based on the role Id which was assigned to user. In the spring release of 2020, Microsoft has provided alternate API to the deprecation of userSettings.securityRoles. Microsoft has replaced this API with Xrm.Utility.getGlobalContext().userSettings.roles.
So, using the updated API we get logged-in user’s security roles with its role Id in the _collection as shown in the screenshot below:
Now, it became easier for developers to get the current user’s security role name and achieve the functionality like show/hide or enable/disable any form attributes programmatically as per requirement. It will save time and line of code of developer as we will get the desired result i.e. role names with this updated Client API.
Using the updated client API, we will get the user’s security role name easily without writing any extra code.