Understanding DynamicEntity in Microsoft Dynamic CRM.

We can use the DynamicEntity to develop against entities and attributes that are not defined at design time.

Or to write code that must work on entities/attributes that would be created/added after the code is deployed.

Let’s take an example of how to use DynamicEntity for creating and updating records using Dlls(Microsoft.crm.sdk and Microsoft.crm.sdktypeproxy) and also by using web service..

Setting up CrmService

CrmAuthenticationToken myToken = new CrmAuthenticationToken();

myToken.OrganizationName = “orgname”;

myToken.AuthenticationType = 0;

CrmService myService = new CrmService();

myService.Credentials = System.Net.CredentialCache.DefaultCredentials;

myService.Url = http://servername:port/MSCrmServices/2007/CrmService.asmx”;

myService.CrmAuthenticationTokenValue = myToken;


Creating a contact record using DynamicEntity inside Microsoft.Crm.Sdk.dll

// Creating an instance of DynamicEntity Class

// Specifying the schema name of Contact entity whose record we are

// going to create

DynamicEntity myDE = new DynamicEntity();

myDE.Name = “contact”;

// Creating an instance of StringProperty to specify firstname attribute value

StringProperty myFirstName = new StringProperty();

myFirstName.Name = “firstname”;

myFirstName.Value = “Nishant”;

// Adding it to DynamicEntity’s properties

myDE.Properties.Add(myFirstName);

// Creating an instance of StringProperty to specify lastname attribute value

StringProperty myLastName = new StringProperty();

myLastName.Name = “lastname”;

myLastName.Value = “Rana”;

myDE.Properties.Add(myLastName);

try

{

contactGuid= myService.Create(myDE);

}

catch (SoapException ex)

{

MessageBox.Show(ex.Detail.InnerXml);

}


Updating a record using DynamicEntity


DynamicEntity myDEUpdate = new DynamicEntity();

myDEUpdate.Name = “contact”;

// Create a KeyProperty to hold the guid of the record to be updated

KeyProperty myContactGuid = new KeyProperty();

myContactGuid.Name = “contactid”;

Key myContactKey=new Key();

myContactKey.Value=contactGuid;

myContactGuid.Value = myContactKey;

myDEUpdate.Properties.Add(myContactGuid);

// Create a StringProperty with the new updated value

StringProperty myFirstNameU = new StringProperty();

myFirstNameU.Name = “firstname”;

myFirstNameU.Value = “Nishu”;

myDEUpdate.Properties.Add(myFirstNameU);

try

{

myService.Update(myDEUpdate);

}

catch (SoapException ex)

{

MessageBox.Show(ex.Detail.InnerXml);

}

The above code while using DynamicEntity class inside the WebService.

Create

DynamicEntity myDE = new DynamicEntity();

myDE.Name = “contact”;

StringProperty myFirstName = new StringProperty();

myFirstName.Name = “firstname”;

myFirstName.Value = “Mickey”;

StringProperty myLastName = new StringProperty();

myLastName.Name = “lastname”;

myLastName.Value = “Mouse”;

// myDE.Properties.Add(myLastName);

myDE.Properties = new Property[] {myFirstName, myLastName };

Guid mycontactGuid = new Guid();

try

{

mycontactGuid= myService.Create(myDE);

}

catch (SoapException ex)

{

MessageBox.Show(ex.Detail.InnerXml);

}

UPDATE

DynamicEntity myDEUpdate = new DynamicEntity();

myDEUpdate.Name = “contact”;

KeyProperty myContactGuid = new KeyProperty();

myContactGuid.Name = “contactid”;

Key myContactKey = new Key();

myContactKey.Value = mycontactGuid;

myContactGuid.Value = myContactKey;

StringProperty myFirstNameU = new StringProperty();

myFirstNameU.Name = “firstname”;

myFirstNameU.Value = “Donald”;

myDEUpdate.Properties = new Property[] { myContactGuid, myFirstNameU };

try

{

myService.Update(myDEUpdate);

}

catch (SoapException ex)

{

MessageBox.Show(ex.Detail.InnerXml);

}

In the same manner we can use Retrieve,Delete method with DynamicEntity.

There are certain Request classes that have a property ReturnDynamicEntities which indicates whether to return the result as a collection of instances of the that Entity or the DynamicEntity class.

Bye..

 


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

10 thoughts on “Understanding DynamicEntity in Microsoft Dynamic CRM.”

  1. Thanks for the sharing it is very usefull who works with crm 4.0.please can you give me advice or a link how is dynamic entity in crm 2013

    Like

  2. Fisrt of all, sorry for my english..

    I need to update Contacts from a CSV file without GUID. I d have an unique colum value name DNI in my CSV.

    Can I retrieve the GUID during import, matching the COntact DNI ?

    Thanks
    Vero

    Like

    1. Hi Nishant,

      Is this deprecated now? we can achieve same using Entity class also. my friend was asking me about DynamicEntity for some interview, I did’t have any idea, because I have started CRM from 2015 version only.

      Please clarify.

      Thanks,
      AshV

      Like

  3. Well I guess you are receiving this error because you may be using CrmService through add web reference and DynamicEntity class within Microsoft.Crm.Sdk. Solution would be to create CrmService through context.CrmService method or use DynamicEntity class within your referenced web service.

    Like

  4. I am prgramming in VB.net, but i think it should make no difference:

    myService.Update(myDEUpdate);
    – gives me Build Error:
    Value of type ‘Microsoft.Crm.Sdk.DynamicEntity’ cannot be converted to ‘WindowsApplication1.CrmWS.BusinessEntity’

    Like

  5. I have a question for you.

    How I can do this: change activities colors depend of the state of activity.

    for example, if the activity is open I use red, if the activity is close, I use blue.

    Can you help me with this ??

    Thanks !!

    Regards

    Roberto

    Like

Share your thoughts

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