Posts Tagged oData in CRM 2011

oData and JScript in CRM 2011

oData and JScript in CRM 2011

Before going through the usage of oData in CRM 2011 first let us understand what is oData and why it is introduced in CRM 2011.

What is oData ?

oData is also referred as Open Data Protocol. CRM 2011 mainly uses Windows Communication Foundation (WCF) data services framework to provide oData Endpoint which is nothing but a REST based data service. The address for the endpoint is:

http://{OrganizationRootUrl} /XRMServices/2011/OrganizationData.svc

Now What is REST ?

REST(Representational State Transfer) is an style of referring data in which every resource can be referenced by a URL mainly in Microsoft CRM. Here resource is nothing but the entity or record or an image etc. So now in CRM 2011 we can reach to the record collection by the URL (properly defined syntax).  Microsoft has provided some syntax styles  which should be followed to access a resource.

oData uses some data  format for sending and receiving the data. Basically it uses the following two formats.

  1. ATOM: It is an Xml based format mainly used for the RSS feeds.
  2. JSON: JavaScript Object Notation is a text formats which makes very easy for the developers to understand the response what we get. For more info please visit:

For eg: For getting all the contact record set the URL can be like

http://{OrganizationRootUrl} /XRMServices/2011/OrganizationData.svc/ContactSet

We can also select few attributes by using a” select “command.

          http://{OrganizationRootUrl} /XRMServices/2011/OrganizationData.svc/ContactSet?$select=FirstName,LastName

This will give a collection of First Name and Last Name for all the contacts. Lot of other stuffs is also possible like filtering, ordering, paging etc.

Now we will see how we can implement in Jscript in CRM 2011. Below is the sample code for fetching the Contact’s PrimaryEmailId form an Account form. Save the code as some js file and upload as a WebResource(Jscript). Refer this in accounts WebResources and call the function OnLoad() in the OnLoad event of account entity.

function OnLoad() {
    var serverUrl = "http://" + + "/" + Xrm.Page.context.getOrgUniqueName();
    var primarycontact = Xrm.Page.getAttribute("primarycontactid").getValue();
    var primarycontactid = primarycontact[0].id;

 // Creating the Odata Endpoint
    var oDataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
    var retrieveReq = new XMLHttpRequest();
    var Odata = oDataPath + "/ContactSet?$select=EMailAddress1&$filter=ContactId eq guid'" + primarycontactid + "'";"GET", Odata, false);
    retrieveReq.setRequestHeader("Accept", "application/json");
    retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    retrieveReq.onreadystatechange = function () { retrieveReqCallBack(this); };

function retrieveReqCallBack(retrieveReq) {
    if (retrieveReq.readyState == 4 /* complete */) {
        var retrieved = this.parent.JSON.parse(retrieveReq.responseText).d;
        var EmailAddress = retrieved.results[0].EMailAddress1;

Test: Create a contact record and put some email id in the Email field. Save the record. Now create one account record and refer this contact in the primarycontact field.  Now whenever you open a account record that has primarycontact the email id will be alerted.      Note: The name of the attributes in the select and the filter criteria are not the actual attributes names but these are the schemas names for that fields so please be careful while referring to the values.Hope this gives a basic idea of oData in CRM 2011…… Happy Reading !!!!!!!!!!!!!!!!!!!!!!!!!!!!!1

, , , ,