web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Nishant Rana’s Weblog / Understanding DynamicEntity...

Understanding DynamicEntity in Microsoft Dynamic CRM.

Nishant Rana Profile Picture Nishant Rana 11,325 Microsoft Employee

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

 

Posted in Microsoft Dynamics CRM Tagged: CRM

This was originally posted here.

Comments

*This post is locked for comments