DynamicEntity Overview

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);

                       

Author: Arvind Singh

Solution Architect with 17+ years of exp. In Dynamics CRM, Power Platform, Azure which includes Solution, Design, Development, Deployment, Maintenance and support experience.

8 thoughts on “DynamicEntity Overview”

    1. 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.

      Like

  1. 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.

    Like

Leave a comment