Get User Security Role Name from context in Dynamics 365 CE
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.
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:
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.
Hiccup Note: While writing this blog I uncovered a bug where the name values are incorrect when an Owner Team is providing additional security roles. There is already a bug in place and is targeting the 4.4 train (end of May 2020) release. In mean time feel test on a development environment in preparation for the bug fix release. I will be updating this blog to remove this statement one the the fix has reached North America. Please check back and/or follow this blog post for updates.
So let’s take a look at a JavaScript sample that uses the new userSettings.roles
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 …