Showing posts with label ODataService. Show all posts
Showing posts with label ODataService. Show all posts

Wednesday, July 27, 2011

Json and Silverlight with ODataService in CRM 2011

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://://xrmservices/2011/organizationdata.svc/OpportunitySet

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 query = (DataServiceQuery_context.OpportunitySet.AddQueryOption("$select", "Name");


$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://://xrmservices/2011/organizationdata.svc/OpportunitySet?$expand=opportunity_customer_accounts,opportunity_customer_contacts&$select=*,opportunity_customer_accounts,opportunity_customer_contacts

Using Silverlight:

To use this with the silverlight you need to write the following query in the silverlight.

DataServiceQuery query = (DataServiceQuery)_context.OpportunitySet.AddQueryOption("$select", "*,opportunity_customer_accounts,opportunity_customer_contacts")
.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:// ://xrmservices/2011/organizationdata.svc/AccountSet?$filter=OwnerId/Id eq (guid'34fca7f5-d556-e011-b9a8-00155d005515')

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 query = (DataServiceQuery)_context.AccountSet.Where(a => a.OwnerId.Id == new Guid("34FCA7F5-D556-E011-B9A8-00155D005515"));

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 = (DataServiceQuery)

query = query.AddQueryOption("$filter", filter);

$orderby: We can use it to set the sort order. Use it same as $filter.

Using JSON:

http:// ://xrmservices/2011/organizationdata.svc/AccountSet?$orderby=Name, Address1_Country.

Using Silverlight:

DataServiceQuery query = (DataServiceQuery)_context.OpportunitySet.AddQueryOption("$orderby", "Name, Address1_Country")


$top: To define the range that how many record want to read. Max limit is 50.

Using JSON:

http:// ://xrmservices/2011/organizationdata.svc/AccountSet?$top=10

Using Silverlight:

DataServiceQuery query = (DataServiceQuery)_context.OpportunitySet.AddQueryOption("$top", 10)


$skip: To define the that how many record want to skip.
Using JSON:

http:// ://xrmservices/2011/organizationdata.svc/AccountSet?$skip=10

Using Silverlight:

DataServiceQuery query = (DataServiceQuery)_context.OpportunitySet.AddQueryOption("$skip", 10)


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 query = (DataServiceQuery)_context.OpportunitySet.AddQueryOption("$skip", _skip).AddQueryOption("$top", _top)