Saturday, 21 May 2016

Odata in MSCRM 2016

Odata is now in a for of Web API MSCRM 2016
exmaple of ODATA
https://URL/accounts?$select=name&$expand=primarycontactid($select=contactid%20,fullname)%20&$filter=primarycontactid/contactid%20eq%20contactId
List accounts starting with A and C
var qry = “accounts?$filter=startswith(name,’A’) or startswith(name,’C’)”;

List accounts owned by a particular user
var qry = “accounts?$select=name,accountnumber,_primarycontactid_value,createdon,accountcategorycode,revenue&$filter=_ownerid_value
Read account details along with the primary contact phone.
We need to use the $expand clause to request data of related entity. However it appears that the $expand clause only works when requesting data of a single record as shown in the below query.

List all Phone Calls of an account
This will display only the subject of the related phone calls.




1function WhoAmIRequest() {
2var clientUrl = Xrm.Page.context.getClientUrl();
3var req = new XMLHttpRequest()
4req.open("GET", encodeURI(clientUrl + "/api/data/v8.0/WhoAmI()"), true);
5req.setRequestHeader("Accept", "application/json");
6req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
7req.setRequestHeader("OData-MaxVersion", "4.0");
8req.setRequestHeader("OData-Version", "4.0");
9req.onreadystatechange = function () {
10if (this.readyState == 4 /* complete */) {
11req.onreadystatechange = null;
12if (this.status == 200) {
13var data = JSON.parse(this.response);
14alert("User Id : "+data.UserId+"\nBusiness Unit Id : "+data.BusinessUnitId+ "\nOrganization Id : "+data.OrganizationId);
15}
16else {
17 
18var error = JSON.parse(this.response).error;
19alert(error.message);
20}
21}
22 
23};
24req.send();
25}