Skip to main content

Notifications

Announcements

No record found.

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.

securityRoles

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:

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

Javascript

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.

Debug catch

By expanding  _collection: we see the running user security role guids …

Collection

And if we scroll to the right we see the corresponding security role name …

Security Role Name

Comments

*This post is locked for comments

  • Ben Fishin Profile Picture Ben Fishin
    Posted at
    Buggs have been addressed. Thanks, -Ben
  • oiluis Profile Picture oiluis 5
    Posted at
    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 ...
  • Community Member Profile Picture Community Member Microsoft Employee
    Posted at
    Want to get newsletter to your mailbox? Subscribe here On Saturday, June 6th, 365 Saturday