How to get user’s security role name using client API in Dynamics 365 CRM

By | June 16, 2020

Introduction

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:

client API in Dynamics 365 CRM

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.

Conclusion

Using the updated client API, we will get the user’s security role name easily without writing any extra code.

Free 70% of storage space in CRM with Attachment Management Apps!

Attach2Dynamics – Store and manage documents/attachments in cloud storage of your choice – SharePoint, Dropbox or Azure Blob Storage from within Dynamics 365 CRM.
SharePoint Security Sync – Robust and secure solution to integrate Dynamics 365 CRM and SharePoint Security Sync thereby ensuring secure access to confidential documents stored in SharePoint.

2 thoughts on “How to get user’s security role name using client API in Dynamics 365 CRM

  1. Romina

    Just adding
    “getAll()”
    at the end of the line:
    Xrm.Utility.getGlobalContext().userSettings.roles.getAll();
    It retrieves the complete Array of roles (with id and name), as an array of lookups.

    1. Inogic

      Hi,

      Please follow this link to install Microsoft Tool for changing Dynamics CRM’s organization setting which also includes removing GUID from the SharePoint folder name.

      Hope this helps.

      Thanks!

Comments are closed.