Dynamics 365 Web API Enhancement

Dynamics 365 released some new enhancements to Web API. If you are new to Web API, I will suggest you refer to our earlier articles for Web API. In this release, create and update Web API requests are enhanced to return an entity object after the record is created or updated. Let’s understand this enhancement, using create request example, mentioned below.

Let's say, we want to create an E-mail activity record, using Web API. The example is mentioned below of using create Web API request , which is used before Dynamics 365.

  1. function createEmail() {  
  2.     var serverURL = Xrm.Page.context.getClientUrl();  
  3.     var email = {};  
  4.     email["subject"] = "Email demo from Web API";  
  5.     email["description"] = "This a web api test";  
  6.     email["[email protected]"] = "/contacts(C41CE33F-D0A0-E611-811E-5065F38C8781)";  
  7.     //activityparty collection   
  8.     var activityparties = [];  
  9.   
  10.     //from party   
  11.     var from = {};  
  12.     from["[email protected]"] = "/systemusers(8D23B2C1-9869-4C3F-9A80-BA51375C1784)";  
  13.     from["participationtypemask"] = 1;  
  14.   
  15.     //to party   
  16.     var to = {};  
  17.     to["[email protected]"] = "/contacts(C41CE33F-D0A0-E611-811E-5065F38C8781)";  
  18.     to["participationtypemask"] = 2;  
  19.   
  20.     activityparties.push(to);  
  21.     activityparties.push(from);  
  22.   
  23.     //set to and from to email   
  24.   
  25.     email["email_activity_parties"] = activityparties;  
  26.     var req = new XMLHttpRequest();  
  27.     req.open("POST", serverURL + "/api/data/v8.0/emails"true);  
  28.     req.setRequestHeader("Accept""application/json");  
  29.     req.setRequestHeader("Content-Type""application/json; charset=utf-8");  
  30.     req.setRequestHeader("OData-MaxVersion""4.0");  
  31.     req.setRequestHeader("OData-Version""4.0");  
  32.     req.setRequestHeader("Prefer""return=representation");  
  33.     req.onreadystatechange = function() {  
  34.         if (this.readyState == 4 /* complete */ ) {  
  35.             req.onreadystatechange = null;  
  36.             if (this.status == 201) {  
  37.                 var emailUri = this.getResponseHeader("OData-EntityId");  
  38.             } else {  
  39.                 var error = JSON.parse(this.response).error;  
  40.                 alert(error.message);  
  41.             }  
  42.         }  
  43.     };  
  44.     req.send(JSON.stringify(email));  
  45. // This is just a sample script. Paste your real code (javascript or HTML) here.  
  46.   
  47. if ('this_is' == /an_example/) {  
  48.     of_beautifier();  
  49. else {  
  50.     var a = b ? (c % d) : e[f];  
  51. }  
When we will execute this request, we can see the screenshot of the watch Window, where we are not getting any reponse, as it’s blank. Older request only returns responseheader, which contains URL of the new record created. You can also see, if it’s returning the status as 204.



Using new Web API enhancement, we can now include additional preference (return=representation) in the header of the request to get an entity object, which is just created, as shown below.

req.setRequestHeader("Prefer”,”return=representation");

Thus, after adding this addition header request, we will get a response, as shown below, and it will also return the status as 201 instead of 204.



We can get an entity object from the response and process fields accordingly, if required.

Stay tuned for more Dynamics 365 new features.


Similar Articles
HIMBAP
We are expert in Microsoft Power Platform.