tkha007
tkha007

Reputation: 212

Dynamics CRM 2011 REST API using jQuery

I personally don't have any experience with CRM 2011 and only recently found out about the REST oData API so decided to have a crack at it using basic jQuery ajax calls. I created a basic html page and put the following code in there from snippets that I picked up from various Google searches. Seems like a fairly straightforward call:

var serverurl = 'http://[OrganizationUrl]/XRMServices/2011/OrganizationData.svc/[PrivateEntity]?$select=[PrivateEntity_Field1],[PrivateEntity_Field2]';

$.ajax({
    beforeSend: function (xhr) {
        xhr.setRequestHeader('Accept', 'application/json');
    },
    url: serverurl,
    type: 'GET',
    dataType: 'jsonp',
    contentType: 'application/json; charset=utf-8',
    success: function (data) {
        alert('success');
    },
    error: function (xhr, status, error) {
        alert("Error : " + status);
    },
}); 

All this does is go to the error callback and alert "Error: parseerror".

A closer inspection on FireBug yields the following request and response headers:Firebug Output

Despite explicitly specifying json in the header and content it still brings back xml/atom feed and still goes the the error callback instead of the success.

Does this JavaScript have to be executed from within CRM i.e. deployed to CRM first? Or can it work as I've done from an external self contained HTML page? I'm familiar with jQuery but not familiar with CRM or this REST API so a little out of my depth here. Any help is appreciated

UPDATE: I've executed the following code in fiddler and it brings back what I expect from CRM:

GET http://[OrganizationUrl]/XRMServices/2011/OrganizationData.svc/[PrivateEntity]?$select=[PrivateEntity_Field1],[PrivateEntity_Field2] HTTP/1.1
User-Agent: Fiddler
Host: melmd0105:5555
Accept:  application/json

Upvotes: 2

Views: 6491

Answers (2)

Daryl
Daryl

Reputation: 18895

I'll agree with Guido's answer that Microsoft says it is limited to Jscript or Silverlight, but I will say that LinqPad has figured out a way to authenticate, and you can actually write linq queries to generate your RestURL. This has been the fastest method for me to generate Rest URLs for CRM 2011 (only works with On Prem).

NOTE I have attempted to use Fiddler to determine what they are doing to authenticate, but with no success.

Here is an SO question using LinqPad and CRM:

How to perform an ODATA expand in LinqPad

Upvotes: 3

Guido Preite
Guido Preite

Reputation: 15138

CRM 2011 REST endpoint is only available for Web Resources, this means that have to be executed from within CRM. Please refer to this page:

http://msdn.microsoft.com/en-us/library/gg334279.aspx

under Limitations you will find this advice:

Use of the REST endpoint is limited to JScript libraries or Silverlight web resources.

Upvotes: 3

Related Questions