Checking for logged in user’s security role is a requirement I haven’t missed in any of my implementations so far. From enterprise implementations to projects spanning couple of months, this requirement I had everywhere. And as much crazy it may sound, until recently there was no way to get the security role names directly using client API. Even the unified interface introduced Xrm.Utility.getGlobalContext().userSettings.securityRoles which unfortunately had the same perennial problem – we shall get a the GUID of the roles user is having and then we have to fire a query to the server side to get the role names.
Thankfully Microsoft have slipped in an update to get the role names. Since this one came without much publicity, it didn’t get the media attention it deserved. Microsoft have deprecated – Xrm.Utility.getGlobalContext().userSettings.securityRoles and instead suggested to use Xrm.Utility.getGlobalContext().userSettings.roles. Use the getAll() function to take the output as array.
As seen from the developer console, it’s a relief certainly. We no longer need to query the separately to get the role names. Wonderful isn’t it. Finally a solution to a long standing problem? Well not exactly.
This method works just fine if you have the security roles directly assigned to the user. However if the user is added to a team and the team have a security role, the name comes as undefined.
Above is a screenshot for a user. The user is assigned to a team and the team is having Sales Manager role. Although the API is indeed returning the GUID of all the roles, the name of the security role coming through team association is coming as undefined.
Honestly I was bit surprised as it is kind of quite basic and this making the final cut with this issue is something I was not expecting.
I hope this is fixed quick by Microsoft.
Till then we wait with bated breath to use this for all scenarios.
Hope this helps!