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.

CRM 4 LINQ examples

Before Microsoft released CRM 2011 they warmed up many of the features like early bound classes and LINQ with later rollups of CRM 4 and the advanced developer extensions.

I have been busy working with CRM 2011 and haven’t yet used early bound classes and linq in CRM 4 yet, as I mainly asked to do bug fixing for the CRM 4 installations we have I tend to just add to/amend what is already there rather than go a bit further and use the advanced developer extensions.  I think I am secretly hoping all the customers will upgrade to CRM 2011 where I know how to do this stuff.  This of course is ignoring the fact you can only use LINQ for on premise installations because you can’t use it with CRM 2011 online.

I saw a great article today on CRM 4 and linq from pogo69 who has recently been writing some excellent blog CRM developer blog posts (keep up the work good sir).

You can read the blog here

It’s basically shows you lots of examples of CRM 4 linq queries which as developer is exactly what you want because you can copy and adjust them, which is how developers work, once you can get an example working then that’s 90 percent of the battle done, unless it’s a really easy example which you then find impossible to change into the complex version of it you need to write.

The blog builds up the complexity of the queries

below is one of the examples but the blog posts goes into great depth and has multiple examples, so if you are thinking about using LINQ in CRM 4 this is a must read and will get you well on your way.

protected class AppointmentDataItem
	// appointment attributes
	public Guid AppointmentId { get; set; }
	public DateTime ScheduledStart { get; set; }
	public DateTime ScheduledEnd { get; set; }
	public DateTime Booked { get; set; }

	// case (job) attributes
	public string JobTicketNumber { get; set; }
	public Guid JobId { get; set; }

	// contact (customer) attributes
	public string CustomerName { get; set; }
	public string CustomerTelephone { get; set; }
	public string CustomerAddress { get; set; }
	public string CustomerCity { get; set; }
	public string CustomerState { get; set; }
	public string CustomerPostcode { get; set; }
	public string CalendarDisplayTitle { get; set; }


List<AppointmentDataItem> appointments =
		from app in xrm.appointments
		join cs in xrm.incidents on app.regardingobjectid.Value equals cs.incidentid
		where cs.msa_partnercontactid.Value == contactId
		select new AppointmentDataItem
				AppointmentId = app.activityid,
				ScheduledStart = activity.scheduledstart.GetValueOrDefault().ToLocalTime(),
				ScheduledEnd = activity.scheduledend.GetValueOrDefault().ToLocalTime(),
				Booked = activity.createdon.GetValueOrDefault().ToLocalTime()


foreach (AppointmentDataItem appointment in appointments)
	// get the Case object to which the Appointment is attached
	var job =
		from cs in xrm.incidents
		join app in xrm.appointments on cs.incidentid equals app.regardingobjectid.Value
		where app.activityid == appointment.AppointmentId
		select cs

	appointment.JobId = job.incidentid;
	appointment.JobTicketNumber = job.ticketnumber;

	// get the Customer Contact to whom the Case is attached
	var customer =
		from cust in xrm.contacts
		join cs in xrm.incidents on cust.contactid equals cs.customerid.Value
		join app in xrm.appointments on cs.incidentid equals app.regardingobjectid.Value
		where app.activityid == appointment.AppointmentId
		select cust

	appointment.CustomerName = customer.fullname;
	appointment.CustomerAddress = customer.address1_line1;
	appointment.CustomerCity = customer.address1_city;
	appointment.CustomerState = customer.address1_stateorprovince;
	appointment.CustomerPostcode = customer.address1_postalcode;
	appointment.CustomerTelephone = customer.telephone2;
	appointment.CalendarDisplayTitle = customer.fullname + " " + FormatTelephone(customer.telephone2) + "<br />" + FormatAddress(activity);