Convert a Dynamic Entity to a System Entity
![]() |
[Applies to: Microsoft Dynamics CRM 4.0]
Find the latest SDK documentation: CRM 2015 SDK
This sample code shows how to convert a dynamic entity instance into a strongly typed account entity instance. The code first creates a dynamic entity, populates its properties with account attributes and values, and then converts the dynamic entity to an account entity.
This sample code can be found in the following files in the SDK download:
Server\HowTo\CS\Entities\ConvertDynamicToCore.cs
Server\HowTo\VB\Entities\ConvertDynamicToCore.vb
For more information about the helper methods in the Microsoft.Crm.Sdk.Utility.CrmServiceUtility namespace, see Utility Sample Code.
Example
[C#]
using System;
using System.Reflection;
using CrmSdk;
using Microsoft.Crm.Sdk.Utility;
namespace Microsoft.Crm.Sdk.HowTo
{
public class ConvertDynamicToCore
{
static void Main(string[] args)
{
// TODO: Change the server URL and organization to match your Microsoft
// Dynamics CRM Server and Microsoft Dynamics CRM organization.
ConvertDynamicToCore.Run("https://localhost:5555", "CRM_SDK");
}
public static bool Run(string crmServerUrl, string orgName)
{
// 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)
{
PropertyInfo entProp = entType.GetProperty(p.Name);
if (null == entProp)
{
Console.WriteLine("Could not find attribute {0} on entity {1}.", p.Name, coreEntityName);
}
else
{
entProp.SetValue(ent, GetAttribute(entity, p.Name), null);
}
}
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)
{
PropertyInfo propInfo = prop.GetType().GetProperty("Value");
return propInfo.GetValue(prop, null);
}
}
return null;
}
else
{
PropertyInfo propInfo = entity.GetType().GetProperty(attribute);
return propInfo.GetValue(entity, null);
}
}
}
}
[Visual Basic .NET]
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic
Imports CrmSdk
Imports Microsoft.Crm.Sdk.Utility
Namespace Microsoft.Crm.Sdk.HowTo
Public Class ConvertDynamicToCore
Sub Main()
' TODO: Change the server URL and organization to match your Microsoft
' Dynamics CRM Server and Microsoft Dynamics CRM organization.
ConvertDynamicToCore.Run("https://localhost:5555", "CRM_SDK")
End Sub
Public Shared Function Run(ByVal crmServerUrl As String, ByVal orgName As String) 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 entProp As PropertyInfo = entType.GetProperty(p.Name)
If entProp Is Nothing Then
Console.WriteLine("Could not find attribute {0} on entity {1}.", p.Name, coreEntityName)
Else
entProp.SetValue(ent, GetAttribute(CType(entity, BusinessEntity), p.Name), Nothing)
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 propInfo As PropertyInfo = prop.GetType().GetProperty("Value")
Return propInfo.GetValue(prop, Nothing)
End If
Next
Return Nothing
Else
Dim propInfo As PropertyInfo = entity.GetType().GetProperty(attribute)
Return propInfo.GetValue(entity, Nothing)
End If
End Function
End Class
End Namespace
See Also
Concepts
Reference
.gif)