CRM 2011 – How to retrieve Users using Javascript

one my favourite CRM bloggers and a very good source of Javascript examples is Jamie Miley, you should definitely check his blog out Life and Technology and subscribe to it.  In fact not only does he consistently write good blog posts but he does it often.

He recently posted a blog post showing how to get users/System Users using Javascript, click on the link below to read the whole thing

How to: Retrieve Users / SystemUsers for an Organization Using Jscript or .NET in Microsoft Dynamics CRM 2011

What I like about Jamie’s blog posts is he shows you the code first in C# and then shows you the code in Javascript.  I’m guessing most CRM developers are C# programmers first and then to use CRM you have to learn the slightly unusual world of scripting and Javascript.  Showing the C# first makes it easier for me to understand the Javascript.

Initially I wondered how useful this code would be especially with CRM 2011 because you can have different forms for different users but then I thought this is the kind of code you don’t realise is useful until you need to do it.  I have also seen recently an example of looking up user roles and if the role was a developer then it would pop a message up warning that testing results will be different because of the extra privileges.

The blog post also had  a useful link to another blog post about parsing Javascript in CRM 2011 compared to CRM 4

To borrow a paragraph or two from the blog post above, he describes how the parsing of Javascript has changed from CRM 4 to CRM 2011.  I advise you to read the whole blog post above if you are interested because it goes into a lot more depth than the snippet below

Now let’s look at how this might be parsed. In CRM 4.0 each attribute had it’s own matching tag name so you could just search for the tag and grab the value using the DOM parsar. That is not the case here as each attribute is wrapped in a then the first child node contains the name of the attribute. The following child nodes contain the data associated with the attribute. For an ntext or a number field you will only have two total subkeys, one for the attribute name and one for the value.

This means we can parse out most simple types above using a function like this:

function parseResponse(responseXML, attributename) { 
debugger; xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); 
xmlDoc.async="false"; xmlDoc.loadXML(responseXML); 
 for (i=0;i<x.length;i++) { 
 if (x[i].childNodes[0].text == attributename) { 
 //we decode the base 64 contents and alert the HTML of the Iframe 


Just pass in the xml from the response containing your entity and your attribute you are looking for.

This function iterates through the attributes by grabbing a collection of attributes and walking through them, searches for our “attributename” in the first child node, and if it’s found, alerts our second child node that represents the value for the attribute.

You will notice that in the XML that there are exceptions to this rule. When it comes to a lookup field (called an “entityreference”) type above you have info representing ID (GUID ID Value), Logical name (Type of entity), and value(usually the name attribute of that entity). You also have attributes of type “BooleanManagedProperty” where you have a “CanBeChanged” child node in between the name of the attribute an the value. I am sure there are more examples but here is how to tackle this.

2 thoughts on “CRM 2011 – How to retrieve Users using Javascript

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.