DynamicEntity Overview

Posted: December 11, 2008 in Microsoft Dynamics CRM
Tags:

A Dynamic Entity consists of a collection of strongly typed properties, much like a Property Bag. Dynamic Entities are used to develop against CRM entities and attributes that are not defined at design time.

When to use Dynamic Entities

 

Use the DynamicEntity class when writing code that must work on entities created after the code is deployed or with attributes added to entities after deployment

 

Example for accessing the accountid attribute using DynamicEntity.

 

dynamicEntity[“accounted”].Value.Value

 

The reason for the Value.Value is because dynEntity [“accountid”].value returns a Property object whose Value property points to the CRM Attribute type which in our case is Key. Key stores the GUID in its Value property.

 

Example of Using DynamicEntity for Create,Retrieve,Update,Delete.

 

Create DynamicEntity

 

DynamicEntity de = new DynamicEntity(“contact”);

StringProperty propFirstName = new StringProperty(“firstname”, “Arvind”);

de.Properties.Add(propFirstName);

StringProperty propLastName = new StringProperty(“lastname”, “Singh”);

de.Properties.Add(propLastName);

m_ID = Service.Create(de);

 

Retrieving a DynamicEntity

 

RetrieveRequest req = new RetrieveRequest();

req.ColumnSet = new AllColumns();

req.ReturnDynamicEntities = true;

TargetRetrieveDynamic target = new TargetRetrieveDynamic();

target.EntityName = “contact”;

target.EntityId = m_ID;

req.Target = target;

RetrieveResponse resp = (RetrieveResponse)service.Execute(req);

DynamicEntity de = (DynamicEntity)resp.BusinessEntity;

Key id = (Key)de.Properties[“contactid”] ;

string firstName = de.Properties[“firstname”].ToString();

string lastName = de.Properties[“lastname”].ToString();

 

Update using DynamicEntity

 

DynamicEntity de = new DynamicEntity(“contact”);

Key key = new Key(m_ID);

KeyProperty keyProp = new KeyProperty(“contactid”, key);

de.Properties.Add(keyProp);

StringProperty propFirstName = new StringProperty(“firstname”, “John”);

de.Properties.Add(propFirstName);

StringProperty propLastName = new StringProperty(“lastname”, “Smith”);

de.Properties.Add(propLastName);

Service.Update(de);

 

Delete

 

Service.Delete(“contact”, m_ID);

                       

Advertisements
Comments
  1. Anjana says:

    Hi Arvind,

    Could you please explain in detail what is the difference between business and dynamic entity. Please explain with 1 example.

  2. Anjana says:

    Or Let me know another way to create a record (without using Dynamic entity) as you have given in above example.

    • Arvind Singh says:

      Dynamic entity is used when you 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 class lets you program against an entity type 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 run time, an exception is thrown.
      Other way to create entity records is using CrmService Web reference. Locate your CrmService Webservice(http:///mscrmservices/2007/crmservice.asmx) and as Web Reference in VS project and use Create method of CrmService Class to create entity record.
      Lots of tutorial is available on net regarding DynamicEntity.
      Hope its help you.

  3. Anjana says:

    Could you please refer/ share any PDF/ link where I can get full tutorial on this.

  4. Anjana says:

    CRM SDK I have and I also got many tutorial regarding dynamic entity. but problem is that I could not get any details regarding business entity. And SDK contains very specific content for any topic not in details :(.

    So I was asking to share any Ebook or PDF related to development parts.

    However Thanks to reply.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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