Getting Formatted Values Using Web API

In our earlier article we discussed how we can fetch data using the retrieve and retrievemultiple requests. We demonstrated how we can include different fields from a primary entity. In this article we are going to discuss how we can fetch additional information (formatted values) and related entity properties.

In our retrieve example we included a single valued navigation property using _navigationpropertyname_value which returns only GUID of the single valued property (lookup field), but if we want to get the text value of the lookup field we can change the request header to include formatted values. Also if you have experience in writing OData query, you might be aware that to get option set text (a very common request) we need to write an additional metadata request using SOAP. But using Web API, we can simply include the following header request, and it will return both option set value and text, if we have any option set field in our query.

  1. req.setRequestHeader("Prefer""odata.include-annotations=OData.Community.Display.V1.FormattedValue");  

So we can modify our earlier request and it will look like the following:

  1. function retrieveEntity(entityName, Id, columnSet) {  
  2.     var serverURL = Xrm.Page.context.getClientUrl();  
  3.     var Query = entityName + "(" + Id + ")" + columnSet;  
  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.   
  11.     req.setRequestHeader("Prefer""odata.include-annotations=OData.Community.Display.V1.FormattedValue");  //for formatted values
  12.   
  13.     req.onreadystatechange = function() {  
  14.         if (this.readyState == 4 /* complete */ ) {  
  15.             req.onreadystatechange = null;  
  16.             if (this.status == 200) {  
  17.                 var data = JSON.parse(this.response);  
  18.                 if (data != null {  
  19.                         alert(data["_primarycontactid_value@OData.Community.Display.V1.FormattedValue"]); //for lookup text  
  20.                         alert(data["[email protected]"]); //for optionset text  
  21.                     }  
  22.                 } else {  
  23.                     var error = JSON.parse(this.response).error;  
  24.                     alert(error.message);  
  25.                 }  
  26.             }  
  27.         };  
  28.         req.send();  
  29.     }  

We can see inthe watch window that it will return details such as the following:

 

Now let’s say we want to get additional properties from related entities, for example, with a query account record, we want to include some of the properties from the primary contact record as well, so to include related entity properties, we can utilize $expend clause just like we can do in OData using the following option:  

  1. var columnSet="?$select=accountnumber&$expand=primarycontactid";  

In above query, we have included  $expend with the name of the single valued navigation property, so as a result, we will get all properties of the primary contact like the following in our response:

 
 
 We can also select which properties we want to get from navigation, such as the following, instead of all (by default it will return all properties):
  1. "?$select=accountnumber,paymenttermscode,address1_city&$expand=primarycontactid($select=firstname,lastname)";  

Similarly, we can fetch data from collection valued navigation properties using a relationship name like the following:

  1. "?$select=accountnumber,paymenttermscode,address1_city&$expand=primarycontactid($select=firstname,lastname),him_building_account($select=him_name)"  
 

In above query him_building_account is the name of N:N relationship that we created in our last article. Apart from returning limited properties we can also apply other clauses like $filter, $orderby and $top in our query for navigation properties.

Stay tuned for more Web API Samples !

Next Recommended Readings