Writing Retrievemultiple Request Using Web API

In our last article we provided sample code to write retrieve method using Web API. Retrieve methods brings data based on the primary key, so we just need to pass primary key of specific entity record whose data we want to bring and obviously it always returns a single record. In this post we are going to provide sample code for retrieving multiple resultset, where we can define query to bring specific data.

Let’s say we want to count number of child contacts records for specific parent account and want to show count on the parent account entity. We do have other options to do this but for our demo purpose let’s say we want to do that using JavaScript.

To write our query, we can use $filter with different operators to check any particular condition, for example in our case to count child contact records we need to query contact entity based on the account lookup present in contact entity. In Web API field of the entities are represented as properties, you can check properties for particular entity here. In contact entity type parent customer lookup represented as _accountid_value so we need to compare current account with this property like the following,

_accountid_value eq e6e7f801-23b3-4398-8ec2-18518a3f9d3d //where GUID represent parent account

Now to count the resultset, we can use $count in our query so directly we can get number of child record. We can create JavaScript web resource and use the following code under text editor:

  1. function retrieveentityCollection(entity, options) {  
  2.     var serverURL = Xrm.Page.context.getClientUrl();  
  3.     var Query = entity + options;  
  4.     var req = new XMLHttpRequest();  
  5.     req.open("GET", serverURL + "/api/data/v8.0/" + Query, true);  
  6.     req.setRequestHeader("Accept""application/json");  
  7.     req.setRequestHeader("Content-Type""application/json; charset=utf-8");  
  8.     req.setRequestHeader("OData-MaxVersion""4.0");  
  9.     req.setRequestHeader("OData-Version""4.0");  
  10.     req.onreadystatechange = function() {  
  11.         if (this.readyState == 4 /* complete */ ) {  
  12.             req.onreadystatechange = null;  
  13.             if (this.status == 200) {  
  14.                 var data = JSON.parse(this.response);  
  15.                 if(data['@odata.count']!=null)  
  16.                 Xrm.Page.getAttribute("numberofemployees").setValue(data['@odata.count']); //we are displaying it in out of the box number o employee field  
  17.             } else {  
  18.                 var error = JSON.parse(this.response).error;  
  19.                 alert(error.message);  
  20.             }  
  21.         }  
  22.     };  
  23.     req.send();  
  24. }  

Now we can write the following method to use retrieveentityCollection method

  1. function AccountOnLoad() {  
  2.     var Id = Xrm.Page.data.entity.getId().substring(1, 37); //remove braces  
  3.     var entity = "contacts";  
  4.     //note here we need to use _accounted_value instead of parentcustomerid  
  5.     var columnSet = "?$select=firstname&$filter=_accountid_value eq " + Id + "&$count=true";  
  6.   retrieveentityCollection(entity, columnSet);  
  7. }  

We need to call AccountOnload method to account entity online load like the following:

  
Stay tuned for more Web API Samples!

Up Next
    Ebook Download
    View all
    Learn
    View all