Earlier, I wrote about using extension methods as a way to really enhance the power of the Dynamic Entity class. In this article, I want to demonstrate utilizing an extension method to generate XML from the data in a dynamic entity object. I’ve done a couple of projects where updated records needed to be sent to a middleware platform for integration into other systems and the XML format is generally my preferred method of doing this.
Schema
First let’s decide what sort of schema we should have the resulting XML look like. You have numerous options here with regards to the XML schema. The main driving factor you go with is how flexible you need the schema to be – very much like the strong-typed entities vs. dynamic entities in CRM.
You could go with something rigid where the XML elements are named after CRM attributes:
<account>
<accountid>{…}</accountid>
<name>Contoso, Inc</account>
</account>
Or you can have generic “attribute” elements with “name” and “value” attributes.
<entity name=”account”>
<attribute name=”accountid” value=”{…}” />
<attribute name=”name” value=”Contoso, Inc” />
</entity>
The individual needs of your specific application would help determine which approach you would take. For purposes of this article, I’ll be taking the more strongly-coupled first approach.
Plug-in Code
Now that we know what the XML is to look like, we’ll do a simple extension method to create some XML that represents the data in our record. We’re going to make a few assumptions:
- We have all the attributes we need already in our dynamic entity object, and
- We want to create a file that contains all of the attributes that are contained in the object.
- We’ll be writing out just the raw values, so no picklist text values or lookup display names. Although these are not difficult, you can easily modify what I present here to suit your needs.
Having said that, let’s get to work.
The basic logic the method follows is simple: Loop through each attribute in the dynamic entity property bag, and write out an XML node using the StringBuilder object. Then return the resulting string:
public static string ConvertToXml(this DynamicEntity entity)
{
StringBuilder xml = new StringBuilder();
// open root element
xml.Append(string.Format("<{0}>",entity.Name));
// loop through properties and write out elements
foreach(Property prop in entity.Properties)
{
string propName = prop.Name;
string propVal = entity.GetStringValue(propName);
xml.Append(string.Format("<{0}>{1}</{0}>", propName, propVal));
}
// close root element
xml.Append(string.Format("</{0}>",entity.Name));
// return results
return xml.ToString();
}
The code does owe some of the simplicity to the GetStringValue() method extension that I use. This method simply takes the attribute name and returns the value of that attribute as a string (for picklists and lookups, it returns the display name instead of the guid or integer value).
Hopefully this sample gives you a good idea of how to quickly and efficiently be able to generate an XML representation of a CRM record for exporting. Cheers!
Maybe it's better to serialize it?
// for DynamicEntity
DynamicEntity dynamicEntity = GetSomeEntity();
XmlSerializer serializer = new XmlSerializer(typeof(DynamicEntity));
StringWriter stringWriter = new StringWriter();
serializer.Serialize(stringWriter, dynamicEntity);
stringWriter.ToString();
// for BusinessEntity
BusinessEntity сontact = GetSomeContact();
XmlSerializer serializer = new XmlSerializer(typeof(contact), "http://schemas.microsoft.com/crm/2007/WebServices");
MemoryStream stream = new MemoryStream();
serializer.Serialize(stream, сontact);
string s = UTF8Encoding.UTF8.GetString(stream.ToArray());
Posted by: Vladislav | November 16, 2009 at 05:12 AM
I had thought about serializng it, but I wanted to take a more basic approach to give more flexibility to the resulting XML.
Also, in one of our integrations we've done, the actual XML elements were aliased - we translated the real CRM schema names to new names that had more meaning for the destination platform. This piece of code allowed us to quickly allow for that.
Posted by: Will Wilson | November 18, 2009 at 07:21 AM
The DynamicEntity XML code that you create by using the Opportunity form differs from the DynamicEntity XML code that you create by using the Marketing List form in Microsoft Dynamics CRM 3.0. This problem occurs even though you use methods that are documented in the Microsoft Dynamics CRM Software Development Kit (SDK) to capture the DynamicEntity XML that is generated when an opportunity is created in Microsoft Dynamics CRM 3.0.
Posted by: 8gb usb drive | December 21, 2009 at 02:28 AM
Nice idea. However, I dont think you can use extension method's with .Net 3.0 (The version of .Net that Crm is compiled under).
It will work well if you manage the server that Crm is installed on, but if it is someone else's server, you cant guarantee that .Net 3.5 will be installed and available.
All the same, Great Post.
Posted by: Web Developer, Machester | March 22, 2010 at 11:38 AM