{"id":868,"date":"2012-11-21T09:19:33","date_gmt":"2012-11-21T09:19:33","guid":{"rendered":"http:\/\/mahenderpal.wordpress.com\/?p=868"},"modified":"2016-08-10T16:04:16","modified_gmt":"2016-08-10T16:04:16","slug":"retrieve-primary-entity-and-related-entity-data-using-odata-in-ms-crm-2011","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=868","title":{"rendered":"Retrieve primary entity and related entity data using OData in MS CRM 2011"},"content":{"rendered":"<p>Are you looking to access primary entity and related entity data in single query using OData then this post is for you.<\/p>\n<p>If we need to access related entity data, we can use <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/gg309461.aspx#BKMK_expand\">expend <\/a>open in our odata query, we need to specify relationship name, in below example I am fetching data from account and more address entity and relationship name for this is \u201cAccount_CustomerAddress\u201d. Here is the code to get entity information:<\/p>\n<p>function GetRelated() {<br \/>\nvar context = Xrm.Page.context;<br \/>\nvar EntityID = Xrm.Page.data.entity.getId();<br \/>\nvar serverUrl = context.getServerUrl();\/\/for CRM 2013 and later use getClientUrl()<br \/>\nvar ODataPath = serverUrl + &#8220;\/XRMServices\/2011\/OrganizationData.svc&#8221;;<br \/>\nvar retrieveResult = new XMLHttpRequest();<br \/>\nretrieveResult.open(&#8220;GET&#8221;, ODataPath + &#8220;\/AccountSet?$filter=AccountId eq (guid'&#8221; + EntityID + &#8220;&#8216;)&amp;$expand=Account_CustomerAddress&#8221;, false);<br \/>\nretrieveResult.setRequestHeader(&#8220;Accept&#8221;, &#8220;application\/json&#8221;);<br \/>\nretrieveResult.setRequestHeader(&#8220;Content-Type&#8221;, &#8220;application\/json; charset=utf-8&#8221;);<br \/>\nretrieveResult.send();<br \/>\nif (retrieveResult.readyState == 4 \/* complete *\/) {<br \/>\nif (retrieveResult.status == 200) {<br \/>\nvar retrieved= this.parent.JSON.parse(retrieveResult.responseText).d;<br \/>\nvar Result = retrieved.results[0];<br \/>\n\/\/Field From Account<br \/>\nalert(Result.Name);<br \/>\n\/\/fields from more address<br \/>\nalert(Result.Account_CustomerAddress.results[0].Line1);<br \/>\n}<br \/>\n}<\/p>\n<p>}<\/p>\n<p>Hope it will help.<\/p>\n<p>Enjoy!!!<br \/>\nNote: if you will copy this code please make sure to change quotes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are you looking to access primary entity and related entity data in single query using OData then this post is for you. If we need to access related entity data, we can use expend open in our odata query, we need to specify relationship name, in below example I am fetching data from account and more address entity and relationship&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=868\">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":[5],"tags":[160],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/868"}],"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=868"}],"version-history":[{"count":2,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/868\/revisions"}],"predecessor-version":[{"id":2375,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/868\/revisions\/2375"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}