Export (0) Print
Expand All

Using DynamicEntity to Access Entities at Runtime

CRM 1.2
banner art

One advantage to using the strongly typed Web Services Description Language (WSDL) is that you can discover and fix many code problems at design time rather than waiting until runtime. However, you may want to write code that accesses entities that are not yet defined. For example, if you write a custom search utility using WSDL, you can only include entities in your search UI that exist in the WSDL that you add to your solution. To work with entities that do not exist at design time, the SDK includes dynamic entity support that does not require regeneration of the WSDL when schema changes are made.

DynamicEntity is a class derived from BusinessEntity that allows an entity instance to be programmed against without having the full definition of the entity in the available WSDL. The DynamicEntity class contains the logical name of the entity and an array of properties for the entity attributes. You can write code that uses this information to work with entities that may not be defined at compile time. If you write code that is looking for an entity that does not exist at runtime, an exception is thrown.

The DynamicEntity class works with the Execute method. It cannot be used with any of the common methods found in the CrmService Web service.

The following table shows the messages that work with DynamicEntity.

Message Target class
Assign TargetOwnedDynamic
Book TargetScheduleDynamic
CompoundCreate TargetCompoundDynamic
CompoundUpdate TargetCompoundDynamic
Create TargetCreateDynamic
Delete TargetDeleteDynamic
GetQuantityDecimal TargetQuantifyDynamic
GrantAccess TargetOwnedDynamic
Handle TargetQueuedDynamic
Merge TargetMergeDynamic
ModifyAccess TargetOwnedDynamic
RemoveRelated TargetRelatedDynamic
Reschedule TargetScheduleDynamic
RetrievePrincipalAccess TargetOwnedDynamic
RetrieveSharedPrincipalsAndAccess TargetOwnedDynamic
Retrieve TargetRetrieveDynamic
RevokeAccess TargetOwnedDynamic
Rollup TargetRollupDynamic
Route TargetQueuedDynamic
Schedule TargetScheduleDynamic
SetRelated TargetRelatedDynamic
SetStateDynamicEntity This message does not use a target class.
Update TargetUpdateDynamic
Validate TargetScheduleDynamic


The following code example shows how to use the Create message with DynamicEntity.

CrmService service = new CrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

WhoAmIRequest userRequest = new WhoAmIRequest();
WhoAmIResponse user = (WhoAmIResponse) service.Execute(userRequest);

DynamicEntity myAccount = new DynamicEntity();
myAccount.Name = "account";

CrmBooleanProperty bulkEmail = new CrmBooleanProperty();
bulkEmail.Name = "donotbulkemail";
bulkEmail.Value = new CrmBoolean();
bulkEmail.Value.Value = true;

OwnerProperty owner = new OwnerProperty();
owner.Name = "ownerid";
owner.Value = new Owner();
owner.Value.Value = user.UserId;
owner.Value.type = SecurityPrincipalType.User.ToString();

CrmMoneyProperty creditLimit = new CrmMoneyProperty();
creditLimit.Name = "creditlimit";
creditLimit.Value = new CrmMoney();
creditLimit.Value.Value = 20000;

StringProperty name = new StringProperty();
name.Name = "name";
name.Value = "Redmond Cycle";

myAccount.Properties = new Property[]
   { name, bulkEmail, owner, creditLimit };

TargetCreateDynamic myTarget = new TargetCreateDynamic();
myTarget.Entity = myAccount;
CreateRequest create = new CreateRequest();
create.Target = myTarget;
CreateResponse myResponse = (CreateResponse)service.Execute(create);


The following code example shows how to use the Retrieve message with DynamicEntity.

CrmService service = new CrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

// Define the target dynamic entity that is to be retrieved.
TargetRetrieveDynamic target = new TargetRetrieveDynamic();
target.EntityId = new Guid("2B951FBC-1C56-4430-B23B-20A1349068F3");
target.EntityName = EntityName.account.ToString();

// Define the entity attributes (database table columns) that are
// to be retrieved.
ColumnSet columnSet = new ColumnSet();
columnSet.Attributes = new string [] { "accountid", "name", 
                                       "modifiedon" };
// Create a retrieve request object.
RetrieveRequest retrieve = new RetrieveRequest();
retrieve.Target = target;
retrieve.ColumnSet = columnSet;
retrieve.ReturnDynamicEntities = true;

// Create a response reference and execute the retrieve request.
RetrieveResponse response = (RetrieveResponse) service.Execute(retrieve);
DynamicEntity retrievedEntity = (DynamicEntity) response.BusinessEntity;

// Access the retrieved properties of the dynamic entity.
foreach (Property prop in retrievedEntity.Properties)
   if (prop is CrmDateTimeProperty)
      // This is a date and time property.
   else if (prop is StringProperty)
      // This is a string property.
[Visual Basic .NET]
Dim service As CrmService = New CrmService
service.Credentials = System.Net.CredentialCache.DefaultCredentials

' Define the target dynamic entity that is to be retrieved.
Dim target As TargetRetrieveDynamic = New TargetRetrieveDynamic
target.EntityId = new Guid("2B951FBC-1C56-4430-B23B-20A1349068F3")
target.EntityId = createResponse.id
target.EntityName = EntityName.account.ToString()

' Define the entity attributes (database table columns) that are to be
' retrieved.
Dim columnSet As columnSet = New columnSet
columnSet.Attributes = New String() {"accountid", "name", "modifiedon"}

' Create a retrieve request object.
Dim retrieve As RetrieveRequest = New RetrieveRequest
retrieve.Target = target
retrieve.ColumnSet = columnSet
retrieve.ReturnDynamicEntities = True

' Create a response reference and execute the retrieve request.
Dim response As RetrieveResponse = CType(service.Execute(retrieve), 
Dim retrievedEntity As DynamicEntity = CType(response.BusinessEntity,

' Access the retrieved properties of the dynamic entity.
Dim prop As [Property]
For Each prop In retrievedEntity.Properties
   If TypeOf prop Is CrmDateTimeProperty Then
      ' This is a date and time property.
   ElseIf TypeOf prop Is StringProperty Then
      ' This is a string property.
   End If

Dynamic Entity Classes

Use these classes for attribute types  when you write code using the DynamicEntity class.

Class name Microsoft CRM Attribute Type Description
CrmBooleanProperty Boolean Represents a boolean attribute.
CrmDateTimeProperty DateTime Represents a date/time attribute .
CrmDecimalProperty Decimal Represents a decimal attribute .
CrmFloatProperty Float Represents a float attribute .
CrmMoneyProperty Money Represents a money attribute .
CrmNumberProperty Integer Represents a number attribute .
CustomerProperty Customer Represents a customer attribute .
DynamicEntity   Represents an instance of an entity.
DynamicEntityArrayProperty CalendarRules


Represents an array of entities.
EntityNameReferenceProperty   Represents an entity name attribute.
KeyProperty PrimaryKey Represents a key attribute.
LookupProperty Lookup Represents a lookup attribute.
OwnerProperty Owner Represents an owner attribute.
PicklistProperty Picklist Represents a picklist attribute.
Property N/A Base class for property types.
StateProperty State Represents a state attribute.
StatusProperty Status Represents a status attribute.
StringProperty String Represents a string attribute.
UniqueIdentifierProperty UniqueIdentifier Represents an attribute that specifies the ID of an entity instance.
N/A Internal



Not mapped in the SDK.

Related Topics

Dynamic Entity Targets

Using Dynamic Entities

© 2007 Microsoft Corporation. All rights reserved.

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