Get User Security Role Name from context in Dynamics 365 CE
Summary
Microsoft Dynamics 365 CE/CRM now has a more streamlined method to get the running users security role name without having to make a client-side API call. In the spring of 2020 there has been an update to the SDK to mark the deprecation of userSettings.securityRoles. It has been replaced with userSettings.roles.
userSettings.roles provides not only the security role guids from the global context, but the security role names as well.
History
A common design pattern in use today is to enable/disable or hide/show client-side form attributes as a part of controlling a business logic form flow. This has been achieved with a JavaScript WebResource in several different patterns. The challenge is that quite a few patterns can cause poor client-side performance, especially in high latency environments. Here are some example conversations from the Microsoft Dynamics CRM Forum community on the subject:
- Get Security role of user using JScript
- MS Dynamics CRM - Login user's security role using javascript
- Retrieve the List of Security Roles Assigned for a Logged-In Dynamics 365 User
If you are fortunate enough to have engaged with a Dynamics 365 CE/CRM DSE or a Microsoft in a Performance Delivery, you may have been provided a sample JavaScript pattern using browser caching that alleviated unnecessary Web API calls.
Sample JavaScript
So let’s take a look at a JavaScript sample that uses the new userSettings.roles
Test it out
Add a JavaScript web resource.
Add the web resource as a reference on a Case form.
Register CaseForm.FromOnload on the Case form Onload event.
Publish the changes.
Identify a test user with security roles for testing.
Open a Case record.
Now we can inspect the Developer Tools (F12) in the browser (example here in Microsoft Edge based on Chromium). Note: you might need to refresh the form with a Shift+F5 to replay the form load.
We see we get the global context and inspect userSettings.roles by hovering the cursor over roles.
By expanding _collection: we see the running user security role guids …
And if we scroll to the right we see the corresponding security role name …
Comments
-
-
It would be perfect if the method wasn't so "buggy" ... In case the user is member of a Team and this Team has roles, the name of the role will be equals to null if you call this method ...
-
*This post is locked for comments