{"id":2557,"date":"2016-11-11T12:36:41","date_gmt":"2016-11-11T12:36:41","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=2557"},"modified":"2016-11-11T12:36:41","modified_gmt":"2016-11-11T12:36:41","slug":"dynamics-365-web-api-enhancement-part-1","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=2557","title":{"rendered":"Dynamics 365 Web API enhancement Part 1"},"content":{"rendered":"<p>Dynamics 365 released some new enhancements to Web API. If you are new to Web API, we will suggest you to refer our earlier <a href=\"https:\/\/himbap.com\/blog\/?s=Web+API\">articles for Web API<\/a>. In this release create and update Web API requests are enhanced to return entity object after record created or updated. Let\u2019s understand this enhancement using following create request example.<\/p>\n<p>Let say we want to create email activity record using Web API. Following is the example of using create Web API request used before Dynamics 365.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction createEmail() {\r\n    var serverURL = Xrm.Page.context.getClientUrl();\r\n    var email = {};\r\n    email[&quot;subject&quot;] = &quot;Email demo from Web API&quot;;\r\n    email[&quot;description&quot;] = &quot;This a web api test&quot;;\r\n    email[&quot;regardingobjectid_contact@odata.bind&quot;] = &quot;\/contacts(C41CE33F-D0A0-E611-811E-5065F38C8781)&quot;;\r\n\r\n    \/\/activityparty collection\r\n    var activityparties = [];\r\n    \/\/from party\r\n    var from = {};\r\n    from[&quot;partyid_systemuser@odata.bind&quot;] = &quot;\/systemusers(8D23B2C1-9869-4C3F-9A80-BA51375C1784)&quot;;\r\n    from[&quot;participationtypemask&quot;] = 1;\r\n\r\n    \/\/to party\r\n    var to = {};\r\n    to[&quot;partyid_contact@odata.bind&quot;] = &quot;\/contacts(C41CE33F-D0A0-E611-811E-5065F38C8781)&quot;;\r\n    to[&quot;participationtypemask&quot;] = 2;\r\n\r\n    activityparties.push(to);\r\n    activityparties.push(from);\r\n\r\n    \/\/set to and from to email\r\n    email[&quot;email_activity_parties&quot;] = activityparties;\r\n\r\n    var req = new XMLHttpRequest();\r\n    req.open(&quot;POST&quot;, serverURL + &quot;\/api\/data\/v8.0\/emails&quot;, true);\r\n    req.setRequestHeader(&quot;Accept&quot;, &quot;application\/json&quot;);\r\n    req.setRequestHeader(&quot;Content-Type&quot;, &quot;application\/json; charset=utf-8&quot;);\r\n    req.setRequestHeader(&quot;OData-MaxVersion&quot;, &quot;4.0&quot;);\r\n    req.setRequestHeader(&quot;OData-Version&quot;, &quot;4.0&quot;);\r\n    req.setRequestHeader(&quot;Prefer&quot;, &quot;return=representation&quot;);\r\n    req.onreadystatechange = function() {\r\n        if (this.readyState == 4 \/* complete *\/ ) {\r\n            req.onreadystatechange = null;\r\n            if (this.status == 201) {\r\n                var emailUri = this.getResponseHeader(&quot;OData-EntityId&quot;);\r\n                }\r\n            else {\r\n                var error = JSON.parse(this.response).error;\r\n                alert(error.message);\r\n            }\r\n        }\r\n    };\r\n    req.send(JSON.stringify(email));\r\n}\r\n<\/pre>\n<p>When we will execute this request, we can see screen shot of watch window, we are not getting any repose, it\u2019s blank. Older request only returns responseheader which contains URI of the new record created. You can see also see it\u2019s returning status as 204.<\/p>\n<p><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/11\/apienhancement2.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2559 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/11\/apienhancement2-300x85.png\" alt=\"apienhancement2\" width=\"300\" height=\"85\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/11\/apienhancement2-300x85.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/11\/apienhancement2-624x177.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/11\/apienhancement2.png 979w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Whereas using new Web API enhancement, we can now include additional preference (return=representation) in the header of the request, to get entity object which is just created like following:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nreq.setRequestHeader(&quot;Prefer\u201d,\u201dreturn=representation&quot;);\r\n<\/pre>\n<p>So after adding this addition header request, we will get response like following and it will also return status as 201 instead of 204.<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/11\/emaiapi1.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2577 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/11\/emaiapi1-300x239.png\" alt=\"emaiapi1\" width=\"300\" height=\"239\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/11\/emaiapi1-300x239.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/11\/emaiapi1-624x498.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/11\/emaiapi1.png 669w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>We can get entity object from response and process fields accordingly if required.<\/p>\n<p>Stay tuned for more Dynamics 365 new features !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dynamics 365 released some new enhancements to Web API. If you are new to Web API, we will suggest you to refer our earlier articles for Web API. In this release create and update Web API requests are enhanced to return entity object after record created or updated. Let\u2019s understand this enhancement using following create request example. Let say we&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=2557\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,402,275,296],"tags":[415,404,414,413],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2557"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2557"}],"version-history":[{"count":6,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2557\/revisions"}],"predecessor-version":[{"id":2579,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2557\/revisions\/2579"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}