{"id":2852,"date":"2017-11-20T12:29:57","date_gmt":"2017-11-20T12:29:57","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=2852"},"modified":"2017-11-20T12:29:57","modified_gmt":"2017-11-20T12:29:57","slug":"using-xrm-webapi-in-dynamics-365crm","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=2852","title":{"rendered":"Using Xrm.WebApi in Dynamics 365\/CRM"},"content":{"rendered":"<p>With the release of Dynamics 365 update 9.0, now we have a new library to implement WebApi methods using <strong>Xrm.WebApi<\/strong>. Instead of writing complete request, now we can just use direct CURD methods from WebApi.<\/p>\n<p>If you are looking for how to write WebApi for Dynamics CRM 2016, check our sample index <a href=\"https:\/\/himbap.com\/blog\/?page_id=2710\">here<\/a><\/p>\n<p><strong>Xrm.WebApi<\/strong> have two properties to use for <strong>Online<\/strong> and <strong>Offline<\/strong> client. In this article we are going to implement a sample HTML webresource using <strong>Xrm.WebApi<\/strong> for online client.<\/p>\n<p>Now to create entity record we can simply call Xrm.WebApi create method using following parameters:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction createAccount() {\r\n    \/\/ collect account data\r\n    var data = {\r\n        &quot;name&quot;: document.getElementById(&quot;txtname&quot;).value,\r\n        &quot;address1_city&quot;: document.getElementById(&quot;txtcity&quot;).value,\r\n        &quot;telephone1&quot;: document.getElementById(&quot;txtphone&quot;).value,\r\n        &quot;numberofemployees&quot;: document.getElementById(&quot;txttotalemployees&quot;).value,\r\n        &quot;websiteurl&quot;: document.getElementById(&quot;txtwebsite&quot;).value\r\n\r\n    }\r\n    \/\/ create account record\r\n    parent.Xrm.WebApi.createRecord(&quot;account&quot;, data).then(\r\n        function success(result) {\r\n            document.getElementById(&quot;txtaccountid&quot;).value = result.id;\r\n            alert(&quot;Account Created !!&quot;);\r\n        },\r\n        function(error) {\r\n            alert(error.message);\r\n        }\r\n    );\r\n}\r\n<\/pre>\n<p><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/Xrmapi1.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2853 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/Xrmapi1-300x170.png\" alt=\"Xrmapi1\" width=\"300\" height=\"170\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/Xrmapi1-300x170.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/Xrmapi1-624x353.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/Xrmapi1.png 895w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>To update entity record we can use following method and pass updated data:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction updateAccount() {\r\n    \/\/ collect account data\r\n    var data = {\r\n        &quot;name&quot;: document.getElementById(&quot;txtname&quot;).value,\r\n        &quot;address1_city&quot;: document.getElementById(&quot;txtcity&quot;).value,\r\n        &quot;telephone1&quot;: document.getElementById(&quot;txtphone&quot;).value,\r\n        &quot;numberofemployees&quot;: document.getElementById(&quot;txttotalemployees&quot;).value,\r\n        &quot;websiteurl&quot;: document.getElementById(&quot;txtwebsite&quot;).value\r\n\r\n    }\r\n    \/\/get account id\r\n    var accountId = document.getElementById(&quot;txtaccountid&quot;).value;\r\n\r\n    \/\/ update account record\r\n    parent.Xrm.WebApi.updateRecord(&quot;account&quot;, accountId, data).then(\r\n        function success(result) {\r\n            alert(&quot;Account Record Updated&quot;);\r\n\r\n        },\r\n        function(error) {\r\n            alert(error.message);\r\n\r\n        }\r\n    );\r\n\r\n}\r\n<\/pre>\n<p><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/Xrmapi2.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2854 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/Xrmapi2-300x121.png\" alt=\"Xrmapi2\" width=\"300\" height=\"121\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/Xrmapi2-300x121.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/Xrmapi2-624x252.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/Xrmapi2.png 796w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>And to delete we can simply pass entity and record it like following:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction deleteAccount() {\r\n     var accountId = document.getElementById(&quot;txtaccountid&quot;).value;\r\n     parent.Xrm.WebApi.deleteRecord(&quot;account&quot;, accountId).then(\r\n         function success(result) {\r\n             alert(&quot;Account deleted&quot;);\r\n\r\n         },\r\n         function(error) {\r\n             alert(error.message);\r\n\r\n         }\r\n     );\r\n }\r\n<\/pre>\n<p><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/Xrmapi3.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2855 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/Xrmapi3-300x177.png\" alt=\"Xrmapi3\" width=\"300\" height=\"177\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/Xrmapi3-300x177.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/11\/Xrmapi3.png 598w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>You can download complete code from <a href=\"https:\/\/github.com\/himbap\/Samplecode\">GitHub here<\/a><br \/>\nStay Tuned for more Dynamics 365 Update 9.0 features !!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With the release of Dynamics 365 update 9.0, now we have a new library to implement WebApi methods using Xrm.WebApi. Instead of writing complete request, now we can just use direct CURD methods from WebApi. If you are looking for how to write WebApi for Dynamics CRM 2016, check our sample index here Xrm.WebApi have two properties to use for&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=2852\">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":[402],"tags":[481,478,480,479],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2852"}],"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=2852"}],"version-history":[{"count":4,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2852\/revisions"}],"predecessor-version":[{"id":2859,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2852\/revisions\/2859"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}