Freigeben über


How to convert DynamicEntity to Strongly typed Business Entity

As i mentioned in earlier blogs, Business Entities in Microsoft Dynamics CRM V3.0 can be represented using two different classes. For example an Account Business Entity can be represented using a DynamicEntity class instance or an account class instance. The difference is that the former has an array of strongly typed properties whereas the later is a class that has strongly typed properties directly on the class.

One of the common questions that i get asked is how can i convert a DynamicEntity into a strongly typed class. Here is an easy way to convert an account buisness entity in form of DynamicEntity into a strongly typed account class instanace.  

Here are some basic steps to get the code working:

  1. Start a new Visual Studio .NET C# project and call it ConvertDynamicEntity
  2. Add the CRM WSDL(a.k.a. web reference) to your project using the WSDL using "Add a Web Reference" feature of VS. Use "CRM" as the name of your new web reference. 
  3. Copy and paste this code into to your main class and you should be able to do the conversion. 

Happy coding:-)

//An example sample code to convert an Account in for of DynamicEntity to an strongly typed business entity class 'account'

public void ConvertDynamicEntitytoAccount()

{

   DynamicEntity accountDynamicEntity = new DynamicEntity();

   //Set a few properties for test

   StringProperty name = CrmTypes.CreateStringProperty("name", "Fabrikam Inc.");

   StringProperty accountnumber = CrmTypes.CreateStringProperty("accountnumber", "AZ1200");

   PicklistProperty shippingmethodcode = CrmTypes.CreatePickListProperty("address1_shippingmethodcode", CrmTypes.CreatePickList(2));

   accountDynamicEntity.Name = EntityName.account.ToString();

   accountDynamicEntity.Properties = new Property[] { name, accountnumber, shippingmethodcode };

   //Create a strongly typed class account to copy the Dynamic Entity into

   account coreAccount = new account();

   //Calling the method that converts the Dynamic Entity to strongly typed business entity classes

   coreAccount = (account)ConvertDynamicEntityToCoreEntity(accountDynamicEntity);

   this.textBox1.AppendText(coreAccount.name + " " + coreAccount.accountnumber + " " + coreAccount.address1_shippingmethodcode.Value);

}

//Converts a DynamicEntity to its equivalant strongly typed business entity class

public BusinessEntity ConvertDynamicEntityToCoreEntity(DynamicEntity entity)

{

   string coreEntityName = entity.Name;

   //ConvetDynamicEntity is my project name and CRM is the name of my web reference

   Type entType = Type.GetType("ConvertDynamicEntity.CRM."+coreEntityName);

   ConstructorInfo init = entType.GetConstructor(new Type[] {});

   object ent = init.Invoke(new object[] {});

   foreach(Property p in entity.Properties)

   {

      FieldInfo entField = entType.GetField(p.Name);

      if(null == entField)

      {

         this.textBox1.AppendText("Could not find attribute:"+p.Name+" on entity:"+coreEntityName);

      }

      else

      {

         entField.SetValue(ent, this.GetAttribute(entity, p.Name));

      }

   }

return (BusinessEntity)ent;

}

//Returns an object that represents the value of a Dynamic Entity

public object GetAttribute(BusinessEntity entity, string attribute)

{

   if(entity.GetType() == typeof(DynamicEntity))

   {

      DynamicEntity de = (DynamicEntity)entity;

      foreach(Property prop in de.Properties)

      {

         if (prop.Name == attribute)

         {

               FieldInfo field = prop.GetType().GetField("Value");

               return field.GetValue(prop);

         }

      }

      return null;

   }

   else

   {

       FieldInfo entField = entity.GetType().GetField(attribute);

       return entField.GetValue(entity);

   }

}

}

Comments