Dynamics CRM 2011 now provides the ability to perform data operations on CRM entities using JSON and ODATA service.
Here we have tried to explain the various clauses of JSON that is supported by CRM ODATA and how this service can be used in Silverlight as well as in Scripts. Note not all standard ODATA clauses are supported by CRM.
To use these clauses in silverlight you need to add a reference to the ODataService of the CRM i.e. OrganizationData.svc.
$select: This clause is the Select part of any SQL statement where you can specify the list of attributes to be returned by the query. Note you can also provide the collection property of a 1:N or N:1 relationship.
Using JSON
If you write the following URL to read the opportunity.
http://
it will return all columns of the opportunity in XML but if you want to read only the name you can add the select as follows.
http://ad12:5555/crmorg1//xrmservices/2011/organizationdata.svc/OpportunitySet?$select=Name
It will return the folliwng XML
Using Silverlight:
To use the $select in the Silverlight app you need to write the query as follows.
DataServiceQuery
$expand: Directs that related records should be retrieved in the record or collection being retrieved.
Using JSON:
If you want to read the customer information with the opportunity while we are reading the opportunity by JSON then we can use the expand, and then we can read the customer details with the opportunity. Below we have given the JSON url.
http://
Using Silverlight:
To use this with the silverlight you need to write the following query in the silverlight.
DataServiceQuery
.AddQueryOption(“$expand”, “opportunity_customer_accounts”)
.AddQueryOption(“$expand”, “opportunity_customer_contacts”);
$filter:
Using JSON:
To set the condition in the JSON query we can use the $filter clause in the url.
Suppose if you want read all accounts of an user the JSON url will be as follows.
http://
Using Silverlight:
In silverlight we can use the AddQueryOption to add the $filter as we use for $expand or we can use the Where Condition as shown in the below.
DataServiceQuery
To do it through AddQueryOption you can generate the filter in a string and then pass that string a parameter to the filter option. This provides us to create dynamic filters as shown below
filter += “new_testId eq (guid'” + _userid + “‘) “;
DataServiceQuery
query = query.AddQueryOption(“$filter”, filter);
$orderby: We can use it to set the sort order. Use it same as $filter.
Using JSON:
http://
Using Silverlight:
DataServiceQuery
$top: To define the range that how many record want to read. Max limit is 50.
Using JSON:
http://
Using Silverlight:
DataServiceQuery
$skip: To define the that how many record want to skip.
Using JSON:
http://
Using Silverlight:
DataServiceQuery
NOTE: By default any query executed using ODATA service will only return 50 records at a time. If we want to display All records that exist even if it is more than 50 records, then you can use a combination use of the $skip and $top clauses to read all records.
For this you can recursively call this Query where _top will be const with value 50. And at each recursion we can increase the value of the _skip by 50.
DataServiceQuery