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

 


Discover more from Nishant Rana's Weblog

Subscribe to get the latest posts sent to your email.

Unknown's avatar

Author: Nishant Rana

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

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

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

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

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

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

Please share your thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Nishant Rana's Weblog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Power Platform Puzzles

D365 CRM, Power Platform Tips &Tricks

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Microsoft Business Applications Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog - PPUG.ORG

AI - Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue

The Professional Paraphraser

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

Power Platform Learning

Your Go-To Resource for Power Apps, Power Automate & More

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.