{"id":2077,"date":"2016-01-11T06:54:16","date_gmt":"2016-01-11T06:54:16","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=2077"},"modified":"2016-01-11T06:54:16","modified_gmt":"2016-01-11T06:54:16","slug":"getting-formatted-values-using-web-api","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=2077","title":{"rendered":"Getting Formatted values using Web API"},"content":{"rendered":"<p>In our earlier post we discussed how we can fetch data using <a href=\"https:\/\/himbap.com\/blog\/?p=1997\">retrieve <\/a>and <a href=\"https:\/\/himbap.com\/blog\/?p=2012\">retrievemultiple <\/a>request. We demonstrated how we can include different fields from\u00a0primary entity. In this post we are going to discuss how we can fetch additional information (formatted values) and related entity properties.<\/p>\n<p>In our retrieve example we included single valued navigation property using <strong>_navigationpropertyname_value<\/strong> which returns only GUID of the single valued property (lookup field), but if we want to get text value of the lookup field we can change request header to include formatted values. Also if you have experience in writing OData query, you might be aware of that to get option set text (a very common request)\u00a0we need to write additional metadata request using SOAP. But using Web API, we can simply include following header request, and it will return both option set value and text, if we have any option set field in our query.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nreq.setRequestHeader(&quot;Prefer&quot;, &quot;odata.include-annotations=OData.Community.Display.V1.FormattedValue&quot;);\r\n<\/pre>\n<p>So we can modify our earlier request and it will look like below:<\/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.setRequestHeader(&quot;Prefer&quot;, &quot;odata.include-annotations=OData.Community.Display.V1.FormattedValue&quot;);\r\n    req.onreadystatechange = function() {\r\n        if (this.readyState == 4 \/* complete *\/ ) {\r\n            req.onreadystatechange = null;\r\n            if (this.status == 200) {\r\n                var data = JSON.parse(this.response);\r\n                if (data != null {\r\n                        alert(data[&quot;_primarycontactid_value@OData.Community.Display.V1.FormattedValue&quot;]); \/\/for lookup text\r\n                        alert(data[&quot;paymenttermscode@OData.Community.Display.V1.FormattedValue&quot;]); \/\/for optionset text\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 see in watch window it will return details like below:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/formatedvalues.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2082 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/formatedvalues-300x101.png\" alt=\"formatedvalues\" width=\"300\" height=\"101\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/formatedvalues-300x101.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/formatedvalues-624x210.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/formatedvalues.png 719w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nNow let\u2019s say we want to get additional properties from related entities, for example while 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 <strong>$expend<\/strong> clause just like we can do in OData using following option:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar columnSet=&quot;?$select=accountnumber&amp;$expand=primarycontactid&quot;;\r\n<\/pre>\n<p>In above query, we have included<strong> $expend<\/strong> with the name of the single valued navigation property, so as result, we will get all properties of the primary contact like below in our response:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/addtionalvalues1.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2083 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/addtionalvalues1-300x180.png\" alt=\"addtionalvalues1\" width=\"300\" height=\"180\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/addtionalvalues1-300x180.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/addtionalvalues1-624x374.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/addtionalvalues1.png 663w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>We can also select which properties we want to get from navigation like below, instead of all (by default it will return all properties):<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&quot;?$select=accountnumber,paymenttermscode,address1_city&amp;$expand=primarycontactid($select=firstname,lastname)&quot;;\r\n<\/pre>\n<p>Similarly we can fetch data from collection valued navigation properties using relationship name like below:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&quot;?$select=accountnumber,paymenttermscode,address1_city&amp;$expand=primarycontactid($select=firstname,lastname),him_building_account($select=him_name)&quot;\r\n<\/pre>\n<p><a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/retrievemultiple.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2086 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/retrievemultiple-300x87.png\" alt=\"retrievemultiple\" width=\"300\" height=\"87\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/retrievemultiple-300x87.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2016\/01\/retrievemultiple.png 612w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nIn above query <strong>him_building_account<\/strong> is the name of <strong>N:N<\/strong> relationship that we created in <a href=\"https:\/\/himbap.com\/blog\/?p=2063\">our last post<\/a>.Apart from returning limited properties we can also apply other clauses like <strong>$filter, $orderby<\/strong> and <strong>$top<\/strong> in our query for navigation properties.<\/p>\n<p>Stay tuned for more Web API Samples !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our earlier post we discussed how we can fetch data using retrieve and retrievemultiple request. We demonstrated how we can include different fields from\u00a0primary entity. In this post we are going to discuss how we can fetch additional information (formatted values) and related entity properties. In our retrieve example we included single valued navigation property using _navigationpropertyname_value which returns&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=2077\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":2086,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,275,296],"tags":[323,321,322,320,324],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2077"}],"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=2077"}],"version-history":[{"count":12,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2077\/revisions"}],"predecessor-version":[{"id":2092,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2077\/revisions\/2092"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/media\/2086"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}