How to Convert Entity Collection into XML in Dynamics 365 CRM

By | September 9, 2019

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 and send it as response.

Please find below code to convert entity collection into XML format.

Steps 1: Connect to CRM
Steps 2: Retrieve collection of lead records

Here collection is a list of lead records.

Code to convert entity collection into XML:

 //Convert Entity Collection records into XML 
var xEle = new XElement("LeadDetails",
from lead in collection.Entities
select new XElement("Lead",
       new XElement("Topic", (lead.Attributes.Contains("subject") && lead["subject"] != null) ? lead["subject"] : ""),
       new XElement("StatusReason", (lead.Attributes.Contains("statuscode") && lead["statuscode"] != null) ? lead.FormattedValues["statuscode"] : ""),
       new XElement("Salutation", (lead.Attributes.Contains("new_salutationn") && lead["new_salutationn"] != null) ? lead.FormattedValues["new_salutationn"] : ""),
       new XElement("Province", (lead.Attributes.Contains("address1_stateorprovince") && lead["address1_stateorprovince"] != null) ? lead["address1_stateorprovince"] : ""),
       new XElement("Language", (lead.Attributes.Contains("new_language") && lead["new_language"] != null) ? lead.FormattedValues["new_language"] : ""),
       new XElement("Owner", (lead.Attributes.Contains("ownerid") && lead["ownerid"] != null) ? lead.FormattedValues["ownerid"] : ""),
       new XElement("City", (lead.Attributes.Contains("address1_city") && lead["address1_city"] != null) ? lead["address1_city"] : ""),
       new XElement("Status", (lead.Attributes.Contains("statecode") && lead["statecode"] != null) ? lead.FormattedValues["statecode"] : ""),
       new XElement("PostalCode", (lead.Attributes.Contains("address1_postalcode") && lead["address1_postalcode"] != null) ? lead["address1_postalcode"] : ""),
       new XElement("LeadSource", (lead.Attributes.Contains("leadsourcecode") && lead["leadsourcecode"] != null) ? lead.FormattedValues["leadsourcecode"] : ""),
       new XElement("LeadUniqueId", lead.Id.ToString())
                           ));
 //return XML with 200 status code from Azure function
return new HttpResponseMessage(HttpStatusCode.OK)
{
               Content = new StringContent(xEle.ToString(), Encoding.Default, @"application/xml"),
};

We will get following output when we execute HTTP request.

This XML file does not appear to have any style information associated with it. The document tree is shown below:

New
    
    
    Français
    System Admin
    
    Open
    
    
    ..................................
  
  
    
    New
    
    
    English
    System Admin
    
    Open
    
    
    ...................................
  
  
    
    New
    
    
    Français
    System Admin
    
    Open
    
    
    ..................................
  
  .
  .
  .
  .
  .
  .
  .
  .

Conclusion

In this way, with the use of above code you will be able to convert Entity Collection into XML.

Leave a Reply

Your email address will not be published. Required fields are marked *