Serialization: CRM entities vs DynamicEntities


I did a quick experiment to see what the serialization formats look like for a DynamicEntity and a static entity type as found in the Microsoft.Crm.SdkTypeProxy namespace. The results were predictable by looking at the structure of each type of entity, but I thought that I’d post my results here if for no other reason than to provide you with some canned CRM data that you can use for testing, which was the entire point of this exercise for me. I also wanted to know if CRM entities could be ’round-tripped’ — that is, serialized to xml and then deserialized back to entity instances. The answer is yes, they certainly can. So we can grab some data from a live CRM installation and use it in offline tests.

The following code illustrates the concept of using .NET serialization to round-trip a .NET type (a CRM entity in our specific case):

	public static Object RoundTrip( Type in_type, Object in_instance ) {
			System.Xml.Serialization.XmlSerializer xser = new System.Xml.Serialization.XmlSerializer( in_type );
			MemoryStream ms = new MemoryStream();
			xser.Serialize( ms, in_instance );
			ms.Seek( 0, SeekOrigin.Begin );

			Object retval = xser.Deserialize( ms );
			return retval;

When we save the intermediate results to disk, we get a file like this for a static product type:

<?xml version="1.0"?>
<product xmlns:xsi="" xmlns:xsd="">
  <createdby name="First name Last name" dsc="0" xmlns="">3bbf2574-c324-df11-b753-0800277bde92</createdby>
  <createdon date="6/22/2010" time="3:06 PM" xmlns="">2010-06-22T15:06:20-04:00</createdon>
  <defaultuomid name="Primary Unit" dsc="0" xmlns="">36d207f3-3d9e-4e29-abf7-de19896e78dc</defaultuomid>
  <defaultuomscheduleid name="Default Unit" dsc="0" xmlns="">999cdc43-8207-42f5-b15b-f4afdaee3f0f</defaultuomscheduleid>
  <exchangerate formattedvalue="1.0000000000" xmlns="">1</exchangerate>
  <iskit name="No" xmlns="">false</iskit>
  <isstockitem name="No" xmlns="">false</isstockitem>
  <modifiedby name="First name Last name" dsc="0" xmlns="">3bbf2574-c324-df11-b753-0800277bde92</modifiedby>
  <modifiedon date="6/22/2010" time="5:14 PM" xmlns="">2010-06-22T17:14:49-04:00</modifiedon>
  <name xmlns="">Test Product</name>
  <organizationid name="danco" dsc="0" xmlns="">c1c701e4-f69c-4363-8dba-41aa5d5754ac</organizationid>
  <pricelevelid name="My Price List" dsc="0" xmlns="">4098399d-317e-df11-a711-0800277bde92</pricelevelid>
  <productid xmlns="">40b0ae3d-317e-df11-a711-0800277bde92</productid>
  <productnumber xmlns="">PROD1</productnumber>
  <producttypecode name="Sales Inventory" xmlns="">1</producttypecode>
  <quantitydecimal formattedvalue="2" xmlns="">2</quantitydecimal>
  <statecode formattedvalue="Active" xmlns="">Active</statecode>
  <statuscode name="Active" xmlns="">1</statuscode>
  <transactioncurrencyid name="US Dollar" dsc="0" xmlns="">447adec9-c324-df11-b753-0800277bde92</transactioncurrencyid>

The DynamicEntity looks like this:

<?xml version="1.0"?>
<DynamicEntity xmlns:xsi="" xmlns:xsd="" Name="product">
  <Properties xmlns="">
    <Property xsi:type="StateProperty" Name="statecode">
    <Property xsi:type="KeyProperty" Name="productid">
    <Property xsi:type="CrmBooleanProperty" Name="iskit">
      <Value name="No">false</Value>
    <Property xsi:type="CrmDateTimeProperty" Name="createdon">
      <Value date="6/22/2010" time="3:06 PM">2010-06-22T15:06:20-04:00</Value>
    <Property xsi:type="CrmDateTimeProperty" Name="modifiedon">
      <Value date="6/22/2010" time="5:14 PM">2010-06-22T17:14:49-04:00</Value>
    <Property xsi:type="LookupProperty" Name="defaultuomscheduleid">
      <Value name="Default Unit" type="uomschedule" dsc="0">999cdc43-8207-42f5-b15b-f4afdaee3f0f</Value>
    <Property xsi:type="PicklistProperty" Name="producttypecode">
      <Value name="Sales Inventory">1</Value>
    <Property xsi:type="StringProperty" Name="name">
      <Value>Test Product</Value>
    <Property xsi:type="CrmBooleanProperty" Name="isstockitem">
      <Value name="No">false</Value>
    <Property xsi:type="StatusProperty" Name="statuscode">
      <Value name="Active">1</Value>
    <Property xsi:type="LookupProperty" Name="transactioncurrencyid">
      <Value name="US Dollar" type="transactioncurrency" dsc="0">447adec9-c324-df11-b753-0800277bde92</Value>
    <Property xsi:type="CrmDecimalProperty" Name="exchangerate">
      <Value formattedvalue="1.0000000000">1.0000000000</Value>
    <Property xsi:type="LookupProperty" Name="pricelevelid">
      <Value name="My Price List" type="pricelevel" dsc="0">4098399d-317e-df11-a711-0800277bde92</Value>
    <Property xsi:type="LookupProperty" Name="modifiedby">
      <Value name="First name Last name" type="systemuser" dsc="0">3bbf2574-c324-df11-b753-0800277bde92</Value>
    <Property xsi:type="StringProperty" Name="productnumber">
    <Property xsi:type="LookupProperty" Name="defaultuomid">
      <Value name="Primary Unit" type="uom" dsc="0">36d207f3-3d9e-4e29-abf7-de19896e78dc</Value>
    <Property xsi:type="CrmNumberProperty" Name="quantitydecimal">
      <Value formattedvalue="2">2</Value>
    <Property xsi:type="LookupProperty" Name="createdby">
      <Value name="First name Last name" type="systemuser" dsc="0">3bbf2574-c324-df11-b753-0800277bde92</Value>
    <Property xsi:type="LookupProperty" Name="organizationid">
      <Value name="danco" type="organization" dsc="0">c1c701e4-f69c-4363-8dba-41aa5d5754ac</Value>

I’ll be following up with how I’m using this in my mock CRM implementation that I’m going to use for testing.


No Responses Yet to “Serialization: CRM entities vs DynamicEntities”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: