{"id":1997,"date":"2015-12-28T10:45:58","date_gmt":"2015-12-28T10:45:58","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=1997"},"modified":"2016-01-05T08:49:45","modified_gmt":"2016-01-05T08:49:45","slug":"writing-retrieve-method-using-web-api","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=1997","title":{"rendered":"Writing retrieve method using Web API"},"content":{"rendered":"<p>With the release of Dynamics CRM 2016, organization data services is depreciated, so going forward we need to make sure to write scripting code using Web API only. Web API implements OData V4 and can be used for any operation that we can be done using organization service, so now we can use Web API to get both data and metadata. In this blog post we are providing retrieve method sample code<\/p>\n<p>To use Web API we need to use URL like below<br \/>\n<em><br \/>\n[Organization URI] +&#8221;\/api\/data\/v8.0\/&#8221;<\/em>  <\/p>\n<p>Let\u2019s take an example we want to retrieve entity specific attributes based on primary entity id, so we can create script web resource and use following code:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction retrieveEntity(entityname,id, columnset) {\r\n    var serverURL = Xrm.Page.context.getClientUrl();\r\n    var Query = entityname+ &quot;(&quot; + id + &quot;)&quot; + columnset;\r\n    var req = new XMLHttpRequest();\r\n    req.open(&quot;GET&quot;, serverURL + &quot;\/api\/data\/v8.0\/&quot; + Query, 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.onreadystatechange = function() {\r\n        if (this.readyState == 4 \/* complete *\/ ) {\r\n            req.onreadystatechange = null;\r\n            if (this.status == 200)\/\/to verify result is OK {\r\n                var data = JSON.parse(this.response);\r\n               if(data!=null &amp;&amp; data.accountnumber!=null)\r\n\t                alert(data.accountnumber);\r\n               if(data!=null &amp;&amp; data._primarycontactid_value!=null)\r\n                      alert(data._primarycontactid_value); \/\/for lookup id\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();\r\n}\r\n<\/pre>\n<p>We can consume this method using following code:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar Id=entityid; \/\/primary key (GUID)\r\nvar entityName=&quot;accounts&quot;; \r\nvar columnSet=&quot;?$select=accountnumber,address1_city,_primarycontactid_value&quot;; \/\/list of column that we want to fetch\r\nretrieveEntity(entityName,Id,columnSet);\r\n<\/pre>\n<p>This code can be modified to work with any entity and to bring attributes based on requirement.You can get out of the box entity name and attributes reference from <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/mt608066.aspx\">here<\/a><\/p>\n<p>Stay tuned for more Web API Samples !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With the release of Dynamics CRM 2016, organization data services is depreciated, so going forward we need to make sure to write scripting code using Web API only. Web API implements OData V4 and can be used for any operation that we can be done using organization service, so now we can use Web API to get both data and&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=1997\">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":[275,296],"tags":[299,298,297],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1997"}],"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=1997"}],"version-history":[{"count":13,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1997\/revisions"}],"predecessor-version":[{"id":2062,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1997\/revisions\/2062"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}