You may be asking? Why this mundane post? After all we have been here close to 8 years since 2011 released and we have millions of time retrieved it using Xrm.Page.context.getUserRoles().
So what’s the fuss in it?
So if you are working on CRM version below 9.0, then it’s no fuss. But if you are working on V9.0 and above, may be this is an interesting read for you.
Xrm.Page is deprecated. So you should no longer be using Xrm.Page.context.getUserRoles()
So what’s the other way. Well you need to do it the V9.0 way. Below is the code to do the same.
var userRoles = Xrm.Utility.getGlobalContext().userSettings.securityRoles;
And the userRoles variable here contains the list of GUID’s of the roles the user have. Wasn’t that easy?
For e.g – not only you can get the security roles but also the privilege id’s for all the security roles associated with the user.
var userRolesPrivileges = Xrm.Utility.getGlobalContext().userSettings.securityRolePrivileges;
Unfortunately, the above line only works in UCI and not on the web version.