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

                       

%d bloggers like this: