{"id":386,"date":"2014-03-31T11:35:37","date_gmt":"2014-03-31T06:05:37","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=386"},"modified":"2021-12-06T15:02:53","modified_gmt":"2021-12-06T09:32:53","slug":"use-of-alias-in-fetchxml","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2014\/03\/use-of-alias-in-fetchxml\/","title":{"rendered":"Use of Alias in FetchXML"},"content":{"rendered":"<p><b><span style=\"text-decoration: underline;\">Introduction<\/span><\/b>:<\/p>\n<p>I am sure by now almost anyone developing on Dynamics CRM would have had experience working with FetchXML queries, the Dynamics CRM proprietary format to query data using Dynamics CRM SDK API.<\/p>\n<p><span style=\"line-height: 1.5em;\">We recently came across a scenario where we needed to use an alias for an attribute. Similar to the SQL query<\/span><\/p>\n<p><span style=\"line-height: 1.5em;\">Select fullname as ContactName from Contact<\/span><\/p>\n<p><span style=\"line-height: 1.5em;\">Our search to get this work with FetchXML led us to the finding that this is indeed possible!!!<\/span><\/p>\n<p>Let me first give a background on how alias has been generally used by us.<\/p>\n<p><b style=\"line-height: 1.5em;\"><span style=\"text-decoration: underline;\">Use of Alias in Link entities:<\/span><\/b><\/p>\n<p><span style=\"line-height: 1.5em;\">To design queries that require you to join one or more tables, we make use of Link-Entity. The Link-Entity syntax requires providing of an Alias using which the values of the attributes can be accessed from the resultset. If you do not provide an explicit alias, the platform would auto generate the alias for you.<\/span><\/p>\n<p>Here is sample fetchxml that will read the details of the invoice and the related customer be it account or contact.<\/p>\n<p>string query = @&#8221;&lt;fetch\u00a0version=&#8217;1.0&#8242;\u00a0output-format=&#8217;xml-platform&#8217;\u00a0mapping=&#8217;logical&#8217;\u00a0distinct=&#8217;false&#8217;&gt;<\/p>\n<p>&lt;entity\u00a0name=&#8217;invoice&#8217;&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;name&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;customerid&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;statuscode&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;totalamount&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;invoiceid&#8217;\/&gt;<\/p>\n<p>&lt;order\u00a0attribute=&#8217;name&#8217;\u00a0descending=&#8217;false&#8217;\/&gt;<\/p>\n<p>&lt;link-entity\u00a0name=&#8217;account&#8217;\u00a0from=&#8217;accountid&#8217;\u00a0to=&#8217;customerid&#8217;\u00a0link-type=&#8217;outer&#8217;\u00a0alias=&#8217;acc&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0\u00a0name=&#8217;address1_line1&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;address1_line2&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;address1_city&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;address1_country&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;address1_postalcode&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;address1_stateorprovince&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;emailaddress1&#8242;&gt;<\/p>\n<p>&lt;\/link-entity&gt;<\/p>\n<p>&lt;link-entity\u00a0name=&#8217;contact&#8217;\u00a0from=&#8217;contactid&#8217;\u00a0to=&#8217;customerid&#8217;\u00a0link-type=&#8217;outer&#8217;\u00a0alias=&#8217;con&#8217;&gt;<\/p>\n<p>&lt;attribute\u00a0\u00a0name=&#8217;address1_line1&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;address1_line2&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;address1_city&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;address1_country&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;address1_postalcode&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;address1_stateorprovince&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;emailaddress1&#8217;\/&gt;<\/p>\n<p>&lt;link-entity<\/p>\n<p>&lt;\/entity&gt;<\/p>\n<p>&lt;\/fetch&gt; &#8220;;<\/p>\n<p><span style=\"line-height: 1.5em;\">We can use following code to execute fetchxml to Retrieve the results .<\/span><\/p>\n<p><span style=\"line-height: 1.5em;\">\/\/create FetchExpression object and pass fetchxml<\/span><\/p>\n<p>FetchExpression fetchExpression = new FetchExpression(query);<\/p>\n<p><span style=\"line-height: 1.5em;\">\/\/Execute the fetchxml to retrieve records<\/span><\/p>\n<p>EntityCollection entCollection = _service.RetrieveMultiple(fetchExpression);<\/p>\n<p><span style=\"line-height: 1.5em;\">Now here if you would like to read attribute values of Linked entities then it can be possible using following ways.<\/span><\/p>\n<p><span style=\"line-height: 1.5em;\">Read Account entity attributes as follows.<\/span><\/p>\n<p>((AliasedValue)entity.Attributes[&#8220;acc.emailaddress1&#8221;]).Value<\/p>\n<p><span style=\"line-height: 1.5em;\">Read Contact entity attributes as follows.<\/span><\/p>\n<p>((AliasedValue)entity.Attributes[&#8220;con.emailaddress1&#8221;]).Value<\/p>\n<p><b style=\"line-height: 1.5em;\"><span style=\"text-decoration: underline;\">Use Alias for an attribute<\/span><\/b><\/p>\n<p><span style=\"line-height: 1.5em;\">The examples in SDK always used Alias for aggregation and so it was my understanding that Alias for attributes can only be used in case of aggregation. But simply trying out the below query actually returned desired results.<\/span><\/p>\n<p><span style=\"line-height: 1.5em;\">Here we have to read entity attribute values which can be done using alias as shown below. You can specify alias for each attribute and then read values by referring the Alias provided.<\/span><\/p>\n<p><span style=\"line-height: 1.5em;\">&lt;fetch\u00a0version=&#8217;1.0&#8242;\u00a0output-format=&#8217;xml-platform&#8217;\u00a0mapping=&#8217;logical&#8217;\u00a0distinct=&#8217;false&#8217;&gt;<\/span><\/p>\n<p>&lt;entity\u00a0name=&#8217;invoice&#8217;&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;name&#8217;\u00a0alias=&#8217;inv_name&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;totalamount&#8217;alias=&#8217;inv_amount&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;customerid&#8217;alias=&#8217;inv_customer&#8217;\/&gt;<\/p>\n<p>&lt;order\u00a0attribute=&#8217;name&#8217;\u00a0descending=&#8217;false&#8217;\/&gt;<\/p>\n<p>&lt;\/entity&gt;<\/p>\n<p>&lt;\/fetch&gt;<\/p>\n<p><span style=\"line-height: 1.5em;\">You would now read the attribute values as shown below with the Alias name instead of the attribute name.<\/span><\/p>\n<p><span style=\"line-height: 1.5em;\">((EntityReference)((AliasedValue)entity.Attributes[&#8220;inv_customer&#8221;]).Value).Name<\/span><\/p>\n<p><b style=\"line-height: 1.5em;\"><span style=\"text-decoration: underline;\">Use of Alias for Attribute in Link Entity Queries<\/span><\/b><\/p>\n<p><span style=\"line-height: 1.5em;\">To take this one step further, we decided to test by margining both Link Entity alias and also providing an alias to the attribute read from the Link-Entity to check how that works.<\/span><\/p>\n<p><span style=\"line-height: 1.5em;\">Here following fetch query will show how to give alias names to the link entity attributes. By using this alias names we are able to retrieve the link entities attributes as well.<\/span><\/p>\n<p><span style=\"line-height: 1.5em;\">&lt;fetch\u00a0version=&#8217;1.0&#8242;\u00a0output-format=&#8217;xml-platform&#8217;\u00a0mapping=&#8217;logical&#8217;\u00a0distinct=&#8217;false&#8217;&gt;<\/span><\/p>\n<p>&lt;entity\u00a0name=&#8217;invoice&#8217;&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;name&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;customerid&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;statuscode&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;totalamount&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;invoiceid&#8217;\/&gt;<\/p>\n<p>&lt;order\u00a0attribute=&#8217;name&#8217;\u00a0descending=&#8217;false&#8217;\/&gt;<\/p>\n<p><span style=\"line-height: 1.5em;\">&lt;link-entity\u00a0name=&#8217;account&#8217;\u00a0from=&#8217;accountid&#8217;\u00a0to=&#8217;customerid&#8217;\u00a0link-type=&#8217;outer&#8217;\u00a0alias=&#8217;acc&#8217;&gt;<\/span><\/p>\n<p>&lt;attribute\u00a0\u00a0name=&#8217;address1_line1&#8242;\u00a0alias\u00a0=\u00a0&#8216;Line1&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;address1_line2&#8217;\u00a0alias\u00a0=\u00a0&#8216;Line2&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;address1_city&#8217;\u00a0alias\u00a0=\u00a0&#8216;city&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;address1_country&#8217;\u00a0alias\u00a0=\u00a0&#8216;country&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;address1_postalcode&#8217;\u00a0alias\u00a0=\u00a0&#8216;code&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;address1_stateorprovince&#8217;\u00a0alias\u00a0=\u00a0&#8216;state&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;emailaddress1&#8242;\u00a0alias\u00a0=\u00a0&#8217;email&#8217;\/&gt;<\/p>\n<p>&lt;\/link-entity&gt;<\/p>\n<p><span style=\"line-height: 1.5em;\">&lt;link-entity\u00a0name=&#8217;contact&#8217;\u00a0from=&#8217;contactid&#8217;\u00a0to=&#8217;customerid&#8217;\u00a0link-type=&#8217;outer&#8217;\u00a0alias=&#8217;con&#8217;&gt;<\/span><\/p>\n<p>&lt;attribute\u00a0\u00a0name=&#8217;address1_line1&#8242;\u00a0alias\u00a0=\u00a0&#8216;cont_Line1&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;address1_line2&#8217;\u00a0alias\u00a0=\u00a0&#8216;cont_Line2&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;address1_city&#8217;\u00a0 \/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;address1_country&#8217;\u00a0alias\u00a0=\u00a0&#8216;cont_country&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;address1_postalcode&#8217;\u00a0alias\u00a0=\u00a0&#8216;cont_code&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;address1_stateorprovince&#8217;\u00a0alias\u00a0=\u00a0&#8216;cont_state&#8217;\/&gt;<\/p>\n<p>&lt;attribute\u00a0name=&#8217;emailaddress1&#8242;\u00a0alias\u00a0=\u00a0&#8216;cont_email&#8217;\/&gt;<\/p>\n<p>&lt;\/link-entity&gt;<\/p>\n<p>&lt;\/entity&gt;<\/p>\n<p>&lt;\/fetch&gt;<\/p>\n<p><span style=\"line-height: 1.5em;\">In the first query for link entity provided above, we needed to prefix the attribute with the entity alias to read the values. But here we can directly read the values as follows<\/span><\/p>\n<p><span style=\"line-height: 1.5em;\">((AliasedValue)entity.Attributes[&#8220;cont_city&#8221;]).Value<\/span><\/p>\n<p><span style=\"line-height: 1.5em;\">if the Alias had not been provided at the attribute level the value would be read as<\/span><\/p>\n<p><span style=\"line-height: 1.5em;\">((AliasedValue)entity.Attributes[&#8220;con. address1_city&#8221;]).Value<\/span><\/p>\n<p><b style=\"line-height: 1.5em;\"><span style=\"text-decoration: underline;\">Conclusion:<\/span><\/b><\/p>\n<p>Alias can be used for the Entities in case of Link-Entity as well as for Attributes even if there is no aggregation provided.<\/p>\n<h2 style=\"text-align: left;\"><div class=\"su-heading su-heading-style-default su-heading-align-center\" id=\"\" style=\"font-size:15px;margin-bottom:5px\"><div class=\"su-heading-inner\">One Pic = 1000 words! Analyze data 90% faster with visualization apps!<\/div><\/div><\/h2>\n<p style=\"text-align: left;\"><em>Get optimum visualization of Dynamics 365 CRM data with &#8211;<\/em><br \/>\n<em><strong><a href=\"https:\/\/bit.ly\/3lYvozZ\" target=\"_blank\" rel=\"noopener noreferrer\">Kanban Board<\/a> <\/strong>\u2013 Visualize Dynamics 365 CRM data in Kanban view by categorizing entity records in lanes and rows as per their status, priority, etc.<\/em><br \/>\n<em><strong><a href=\"https:\/\/bit.ly\/3lCSBaA\" target=\"_blank\" rel=\"noopener noreferrer\">Map My Relationships<\/a><\/strong> \u2013 Map My Relationships \u2013 Visualize connections and relationships between Dynamics 365 CRM entities or related records in a Mind Map view.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: I am sure by now almost anyone developing on Dynamics CRM would have had experience working with FetchXML queries, the Dynamics CRM proprietary format to query data using Dynamics CRM SDK API. We recently came across a scenario where we needed to use an alias for an attribute. Similar to the SQL query Select\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2014\/03\/use-of-alias-in-fetchxml\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[21,22,24],"tags":[392,795,797,1390],"class_list":["post-386","post","type-post","status-publish","format-standard","hentry","category-dynamics-crm-2013","category-dynamics-crm-2015","category-dynamics-crm-2016","tag-crm-2013","tag-fetchxml","tag-fetchxml-dynamics-crm","tag-queries"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=386"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/386\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}