Menu

So I’m not going to present anything revolutionary here, but recently I had to do just this. Back in the CRM 4 world I was performing all my web service calls against the SOAP endpoint of CRM, with the move to 2011 I knew the world of REST had arrived but up until a couple of weeks ago I never really had the need to use it in any anger.

So this post is basically how I brought together various resources on the web to get started with REST calls from JavaScript. In some ways this is a bit of brain dump, but hopefully you might find it useful.

So first off some general background information on REST and CRM, I won’t get into too much detail here the information is already on the web and I’d rather not just reproduce it for the sake of filling space.

“REST is an architectural style in which every resource is addressed by using a unique URI. In Microsoft Dynamics CRM, a resource can be an entity collection or a record.”

“Microsoft Dynamics CRM 2011 uses the Windows Communication Foundation (WCF) Data Services framework to provide an Open Data Protocol (OData) endpoint that is a REST-based data service. This endpoint is called the Organization Data Service.”

“The REST endpoint provides an alternative to the WCF SOAP endpoint, but there are currently some limitations.”

In terms of limitations the two (there are more) main parts that jump to mind are:

With REST you can’t do everything SOAP can do. In REST you cannot perform actions that involve; assigning, metadata and execute messages (e.g. CreateRequest). However you can perform all the standard create, retrieve, update and delete (CRUD) calls, and associating and disassociating (AD) records.

CRM’s implementation of OData hasn’t implemented every feature of the OData protocol, the most obvious being ‘count’.

This was basically everything I wanted, it had examples of performing CRUD web service calls, capturing the results and doing things with them.

Even better it has a library ‘SDK.JQuery.js’ which encapsulated all the web service stuff. I’ve used that library quite a bit now and I’ve found it very reliable and if you want to get something moving quickly I would whole heartedly recommend it. If you are more interested in doing this totally from scratch that script should hopefully work as an excellent base for your work.

Make sure to read ‘JQueryRESTDataOperationsSample.js’ which gives an example of how to perform all the operations using the library and parse the results.

Note: For those jQuery adverse there is also a non-jQuery sample on the MSDN.

So the rest of this post is basically how I took that sample and turned it into something I could use easily in CRM, fortunately this isn’t a lot of work.

Copy the contents of ‘SDK.JQuery.js’ into a JavaScript web resource within CRM. This script contains all the web service call stuff.

‘SDK.JQuery.js’ has a dependency on JQuery – its uses the Ajax functionality to perform web service calls. Go to JQuery and download JQuery and load that as a web resource in CRM.

‘SDK.JQuery.js’ will return results as JSON (as opposed to ATOM) so we need to include that as well, go to json2, download and load that into CRM as a web resource.

If you do all that you should end up with something like this:

So to actually use SDK.JQuery create another JavaScript web resource which will hold your code. I’ve named mine Gap.Test, for now put this code into the web resource.

That OData Query Designer doesn’t work when I execute the query in the designer [Arg_SecurityException].

Yeah I got that as well, I don’t actually know how to fix it either. What you can do is copy the query into the web browser URL bar and it will present the results as an ATOM feed. Hopefully this should be enough to get you going. If your browser presents the information as an RSS feed follow the instructions at the start of this article CRM 2011 OData, JSON and CRM Forms.

Knowing how to parse the result set is tricky.

When I started I found the easiest way to do this was to bring back the result and debug them with IE Debugger (F12). You can then use the console to examine the results, once you have done this a few times it becomes easier to predict the result set.

It’s complaining my entity/attribute doesn’t exist.

Make sure you have the right casing. OData is case sensitive and you should be using the ‘schema name’ instead of the ‘name’. E.g. FullName instead of fullname.

Additionally when pass your entity to the library functions don’t include ‘set’ even though it is in the designer – the library automatically appends ‘set’.