Friday 23 March 2012

oData and Jscript in CRM 2011

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

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.
We would be using the JSON format in our example.

The main OData service:

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

1) For retrieving any EntitySet(here GoalSet )
http://{OrganizationRootUrl} /XRMServices/2011/OrganizationData.svc/GoalSet

2) For retrieving limited attributes use $select query option
http://{OrganizationRootUrl}/XRMServices/2011/OrganizationData.svc/GoalSet ?$select=IsAmount,MetricId
This will retrieve collection of IsAmount and MetricId for all Goals.

3) For retrieving data filtered on some criteria use $filter query option
http://{OrganizationRootUrl}/XRMServices/2011/OrganizationData.svc/GoalSet ?$select=IsAmount,MetricId &$filter=GoalId eq guid'" + goalid + "'";
This will retrieve IsAmount and MetricId for the Goal whose GoalId matches with the value of given goalid.

4) For retrieving data from related entity use $expand query option
http://{OrganizationRootUrl}/XRMServices/2011/OrganizationData.svc/GoalSet ?$select=IsAmount,metric_goal/Name,metric_goal/OrganizationId&$expand=metric_goal&$filter=GoalId eq guid'" + goalid + "'";

Here ‘metric_goal’ is the relationship property which defines realtionship between Goal and GoalMetric.

This retrieves IsAmount from GoalSet .Name and Organizationid are fetched from the related GoalMetricSet.

Microsoft Dynamics CRM 2011 does not support querying a multi-level relationship property.Maximum 6 expansion are allowed .

Following is an example for implementing oData using javascript in CRM 2011 to retrieve the required fields from Goal and Goal Metric entities.


As viewed in the snapshot above, three libraries have been included . It is compulsory to include jquery1.4.1.min.js and json2.js for implementing JSON format with oData.Both the files are available in SDK\ samplecode\ js\ restendpoint\restjquerycontacteditor\restjquerycontacteditor \scripts.The third file Goal.js contains two functions as given below i.e. ‘GetIsAmount’ and ‘RetrieveReqCallBack’ which are used to retrieve values for different fields like IsAmount(from Goal entity), Name and OrganisationId(from GoalMetric entity).
function GetIsAmount() {
var goalid =;
var oDataPath = Xrm.Page.context.getServerUrl() + "/xrmservices/2011/organizationdata.svc";
var retrieveReq = new XMLHttpRequest();
var Odata = oDataPath + "/GoalSet?$select=IsAmount,metric_goal/Name,metric_goal/OrganizationId&$expand=metric_goal&$filter=GoalId eq guid'" + goalid + "'";"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) {
var metricId = null;
if (retrieveReq.readyState == 4 /* complete */) {
var retrieved = JSON.parse(retrieveReq.responseText).d;
var isAmount = retrieved.results[0].IsAmount;
var Name = retrieved.results[0].metric_goal.Name;
var OrganizationId = retrieved.results[0].metric_goal.OrganizationId;        

1 comment:

  1. Hi,
    Nice article.

    var Name = retrieved.results[0].metric_goal.Name;

    I think name should be lower case!
    Please correct me if I am wrong!