{"id":20438,"date":"2019-09-09T12:26:12","date_gmt":"2019-09-09T12:26:12","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=20438"},"modified":"2019-09-09T12:26:12","modified_gmt":"2019-09-09T12:26:12","slug":"how-to-convert-entity-collection-into-xml-in-dynamics-365-crm","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2019\/09\/how-to-convert-entity-collection-into-xml-in-dynamics-365-crm\/","title":{"rendered":"How to Convert Entity Collection into XML in Dynamics 365 CRM"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>In this blog we will see how to convert entity collection into XML and send as response in Azure Function in Dynamics 365 CRM.<\/p>\n<p>Recently, one of our client required Lead details in XML format using Http request.<\/p>\n<p>So we created Http Trigger Azure function to retrieve data from CRM and converted into XML and send it as response.<\/p>\n<p>Please find below code to convert entity collection into XML format.<\/p>\n<p>Steps 1: Connect to CRM<br \/>\nSteps 2: Retrieve collection of lead records<\/p>\n<p>Here collection is a list of lead records.<\/p>\n<p>Code to convert entity collection into XML:<\/p>\n<pre class=\"lang:default decode:true \"> \/\/Convert Entity Collection records into XML \r\nvar xEle = new XElement(\"LeadDetails\",\r\nfrom lead in collection.Entities\r\nselect new XElement(\"Lead\",\r\n       new XElement(\"Topic\", (lead.Attributes.Contains(\"subject\") &amp;&amp; lead[\"subject\"] != null) ? lead[\"subject\"] : \"\"),\r\n       new XElement(\"StatusReason\", (lead.Attributes.Contains(\"statuscode\") &amp;&amp; lead[\"statuscode\"] != null) ? lead.FormattedValues[\"statuscode\"] : \"\"),\r\n       new XElement(\"Salutation\", (lead.Attributes.Contains(\"new_salutationn\") &amp;&amp; lead[\"new_salutationn\"] != null) ? lead.FormattedValues[\"new_salutationn\"] : \"\"),\r\n       new XElement(\"Province\", (lead.Attributes.Contains(\"address1_stateorprovince\") &amp;&amp; lead[\"address1_stateorprovince\"] != null) ? lead[\"address1_stateorprovince\"] : \"\"),\r\n       new XElement(\"Language\", (lead.Attributes.Contains(\"new_language\") &amp;&amp; lead[\"new_language\"] != null) ? lead.FormattedValues[\"new_language\"] : \"\"),\r\n       new XElement(\"Owner\", (lead.Attributes.Contains(\"ownerid\") &amp;&amp; lead[\"ownerid\"] != null) ? lead.FormattedValues[\"ownerid\"] : \"\"),\r\n       new XElement(\"City\", (lead.Attributes.Contains(\"address1_city\") &amp;&amp; lead[\"address1_city\"] != null) ? lead[\"address1_city\"] : \"\"),\r\n       new XElement(\"Status\", (lead.Attributes.Contains(\"statecode\") &amp;&amp; lead[\"statecode\"] != null) ? lead.FormattedValues[\"statecode\"] : \"\"),\r\n       new XElement(\"PostalCode\", (lead.Attributes.Contains(\"address1_postalcode\") &amp;&amp; lead[\"address1_postalcode\"] != null) ? lead[\"address1_postalcode\"] : \"\"),\r\n       new XElement(\"LeadSource\", (lead.Attributes.Contains(\"leadsourcecode\") &amp;&amp; lead[\"leadsourcecode\"] != null) ? lead.FormattedValues[\"leadsourcecode\"] : \"\"),\r\n       new XElement(\"LeadUniqueId\", lead.Id.ToString())\r\n                           ));\r\n \/\/return XML with 200 status code from Azure function\r\nreturn new HttpResponseMessage(HttpStatusCode.OK)\r\n{\r\n               Content = new StringContent(xEle.ToString(), Encoding.Default, @\"application\/xml\"),\r\n};\r\n<\/pre>\n<p>We will get following output when we execute HTTP request.<\/p>\n<p>This XML file does not appear to have any style information associated with it. The document tree is shown below:<\/p>\n<pre class=\"lang:default decode:true \">New\r\n    \r\n    \r\n    Fran\u00e7ais\r\n    System Admin\r\n    \r\n    Open\r\n    \r\n    \r\n    ..................................\r\n  \r\n  \r\n    \r\n    New\r\n    \r\n    \r\n    English\r\n    System Admin\r\n    \r\n    Open\r\n    \r\n    \r\n    ...................................\r\n  \r\n  \r\n    \r\n    New\r\n    \r\n    \r\n    Fran\u00e7ais\r\n    System Admin\r\n    \r\n    Open\r\n    \r\n    \r\n    ..................................\r\n  \r\n  .\r\n  .\r\n  .\r\n  .\r\n  .\r\n  .\r\n  .\r\n  .<\/pre>\n<h2>Conclusion<\/h2>\n<p>In this way, with the use of above code you will be able to convert Entity Collection into XML.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In this blog we will see how to convert entity collection into XML and send as response in Azure Function in Dynamics 365 CRM. Recently, one of our client required Lead details in XML format using Http request. So we created Http Trigger Azure function to retrieve data from CRM and converted into XML\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2019\/09\/how-to-convert-entity-collection-into-xml-in-dynamics-365-crm\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":20443,"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":[1],"tags":[1935],"class_list":["post-20438","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-xml-in-dynamics-365-crm"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/20438","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=20438"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/20438\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/20443"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=20438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=20438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=20438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}