As I was producing the associated documentation for my CRM Migration Assistant application, I decided to explore a comparison between a technique that we had to do the â€œhard wayâ€? in CRM 4.0 and a technique that is built into CRM 2011
The technique in question is retrieving a userâ€™s security roles in order to perform some role-specific actions.
//check if the current user has the 'System Administrator' role
//get Current User Roles, oXml is an object
var oXml = GetCurrentUserRoles();
if(oXml != null)
//select the node text
var roles = oXml.selectNodes("//BusinessEntity/q1:name");
if(roles != null)
for( i = 0; i < roles.length; i++)
if(roles[i].text == roleName)
//return true if user has this role
//otherwise return false
I use this code in many different situations to show and hide CRM form elements for specific people.
CRM 2011 SDK Samples
If you open SDK.ContextSamples.js, youâ€™ll see some of the code Iâ€™ll be using today.
Jimâ€™s CRM 4.0 code uses a SOAP call retrieve the security roles for a user. Lucky for us, this functionality is now built into CRM 2011 in the method:
Which returns an array of strings representing the GUID values of each of the security roles that the user is associated with.
This is really great, but I would like to refer to my security roles by name since itâ€™s easier to remember and understand than a GUID. So, I had to add some extra code to handle that requirement.
If (UserHasRole("System Administrator"))
// do something important