Export (0) Print
Expand All

Convert a Dynamic Entity to a Core Entity

CRM 1.2
banner art

This sample code shows how to convert a DynamicEntity object into a strongly typed account entity. The code first creates a DynamicEntity object, populates its properties with account attributes and values, and then converts the dynamic entity to an account entity.

Example

[C#]
using System;
using System.Reflection;
using CrmSdk;

namespace Microsoft.Crm.Sdk.HowTo
{
   class ConvertDynamicToCore
   {
      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      [STAThread]
      static void Main(string[] args)
      {
         Run();
      }

      public static bool Run()
      {
         // Create an account dynamic entity.
         DynamicEntity accountDynamicEntity = new DynamicEntity();

         // Set a few account properties.
         StringProperty name = new StringProperty();
         name.Name = "name";
         name.Value = "Fabrikam Inc.";

         StringProperty accountnumber = new StringProperty();
         accountnumber.Name = "accountnumber";
         accountnumber.Value = "AZ1200";

         Picklist plist = new Picklist();
         plist.name = "UPS";
         plist.Value = 2;

         PicklistProperty shippingmethodcode = new PicklistProperty();
         shippingmethodcode.Name = "address1_shippingmethodcode";
         shippingmethodcode.Value = plist;

         accountDynamicEntity.Properties = new Property[] 
                          { name, accountnumber, shippingmethodcode };
         accountDynamicEntity.Name = EntityName.account.ToString();

         // Create a strongly typed account entity to copy the dynamic
         // entity into.
         account coreAccount = new account();
            
         // Convert the dynamic entity to a strongly typed business
         // entity.
         coreAccount = (account)Convert(accountDynamicEntity);

         Console.WriteLine(
            "\n|Core Account Attributes\n|=======================");
         Console.WriteLine("|name:  {0}", coreAccount.name);
         Console.WriteLine("|accountnumber:  {0}",
                           coreAccount.accountnumber); 
         Console.WriteLine("|address1_shipmethodcode:  {0}",
                           coreAccount.address1_shippingmethodcode.Value);

         return true;
      }

      /// <summary>
      /// Convert a dynamic entity into a strongly typed business entity.
      /// </summary>
      public static BusinessEntity Convert(DynamicEntity entity)
      {
         string coreEntityName = entity.Name;

         Type entType = (new account()).GetType(); 
         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)
            {
               Console.WriteLine(
                  "Could not find attribute {0} on entity {1}.", p.Name,
                  coreEntityName);
            }
            else
            {
               entField.SetValue(ent, GetAttribute(entity, p.Name));
            }
         }
         return (BusinessEntity)ent;
      }

      /// <summary>
      /// This method returns the value of a dynamic entity attribute.
      /// </summary>
      public static 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);
         }
      }
   }
}
[Visual Basic .NET]
Imports System
Imports System.Reflection
Imports CrmSdk

Namespace Microsoft.Crm.Sdk.HowTo

   Class ConvertDynamicToCore

      ' <summary>
      ' The main entry point for the application.
      ' </summary>
      <STAThread()> _
      Public Shared Sub Main(ByVal args As String())
         Run()
      End Sub

      Public Shared Function Run() As Boolean

         ' Create an account dynamic entity.
         Dim accountDynamicEntity As DynamicEntity = New DynamicEntity

         ' Set a few account properties.
         Dim name As StringProperty = New StringProperty
         name.Name = "name"
         name.Value = "Fabrikam Inc."

         Dim accountnumber As StringProperty = New StringProperty
         accountnumber.Name = "accountnumber"
         accountnumber.Value = "AZ1200"

         Dim plist As Picklist = New Picklist
         plist.name = "UPS"
         plist.Value = 2

         Dim shippingmethodcode As PicklistProperty = New PicklistProperty
         shippingmethodcode.Name = "address1_shippingmethodcode"
         shippingmethodcode.Value = plist

         accountDynamicEntity.Properties = New [Property]() _
            {name, accountnumber, shippingmethodcode}
         accountDynamicEntity.Name = EntityName.account.ToString()

         ' Create a strongly typed account entity to copy the dynamic
         ' entity into.
         Dim coreAccount As account = New account

         ' Convert the dynamic entity to a strongly typed business entity.
         coreAccount = CType(Convert(accountDynamicEntity), account)

         Console.WriteLine(vbLf & "|Core Account Attributes" & vbLf & _
                           "|=======================")
         Console.WriteLine("|name:  {0}", coreAccount.name)
         Console.WriteLine("|accountnumber:  {0}", _
                           coreAccount.accountnumber)
         Console.WriteLine("|address1_shipmethodcode:  {0}", _
                           coreAccount.address1_shippingmethodcode.Value)

         Return True
      End Function

      ' <summary>
      ' Convert a dynamic entity into a strongly typed business entity.
      ' </summary>
      Public Shared Function Convert(ByRef entity As DynamicEntity) _
         As BusinessEntity
         Dim coreEntityName As String = entity.Name

         Dim entType As Type = (New account).GetType()
         Dim init As ConstructorInfo = _
            entType.GetConstructor(New Type() {})
         Dim ent As Object = init.Invoke(New Object() {})

         Dim p As [Property]
         For Each p In entity.Properties
            Dim entField As FieldInfo = entType.GetField(p.Name)
            If entField Is Nothing Then
               Console.WriteLine( _
                  "Could not find attribute {0} on entity {1}.", _
                  p.Name, coreEntityName)
            Else
               entField.SetValue(ent, GetAttribute(entity, p.Name))
            End If
         Next

         Return CType(ent, BusinessEntity)
      End Function

      ' <summary>
      ' This method returns the value of a dynamic entity attribute.
      ' </summary>
      Public Shared Function GetAttribute( _
         ByRef entity As BusinessEntity, _
         ByVal attribute As String) As Object

         If entity.GetType().FullName.Equals("CrmSdk.DynamicEntity") Then

            Dim de As DynamicEntity = CType(entity, DynamicEntity)

            Dim prop As [Property]
            For Each prop In de.Properties
               If prop.Name = attribute Then
                  Dim field As FieldInfo = _
                     prop.GetType().GetField("Value")
                  Return field.GetValue(prop)
               End If
            Next
            Return Nothing

         Else
            Dim entField As FieldInfo = _
               entity.GetType().GetField(attribute)
            Return entField.GetValue(entity)
         End If
      End Function

   End Class
End Namespace

© 2007 Microsoft Corporation. All rights reserved.


Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft