Working with Custom Entites in MS CRM 4.0

Working with Custom Entities has been a little tough on me because of the High Learning curve involved.  Finally I did win over and learnt quite a bit about using these entities in custom pages. It’s actually quite simple to work with the custom entities. Here’s a piece of code which will get the custom entity into a dynamic entity for us

//Create of New information the custom entity
DynamicEntity de_Custom = new DynamicEntity();
de_Custom.Name = "CustomEntityName";
de_Custom.Properties = PropertiesCollectionObject;
TargetCreateDynamic tcd_Custom = new TargetCreateDynamic();
tcd_Custom.Entity = de_Custom;
CreateRequest cr_CustomRequest = new CreateRequest();
cr_CustomRequest.Target = tcd_Contact;
CreateResponse cr_CustomResponse = (CreateResponse)
     crm_CreateEntityService.Execute(cr_CustomRequest);

In the second line of the code, is where the magic happens. Here all you have to do is specify the custom/system entity name , when querying the web service, will automatically bind to the entity. PropertiesCollectionObject is a collection of Properties which has all the data (type & value information) which is to be inserted in the entity. Here is an sample of what we will have in the PropertiesCollectionObject

Microsoft.Crm.Sdk.PropertyCollection pc_Properties = new 
     Microsoft.Crm.Sdk.PropertyCollection();
 
StringProperty sp_Prop = new StringProperty();
sp_Prop.Name = "EntityFieldName";
sp_Prop.Value = "ValueToBeInserted;
pc_Properties.Add(sp_Prop);

This property collection will have all the properties like Boolean,Picklist,
Lookup and Number, depending on your requirement. This will get bound with the
property collection in the entity object. When CRMService executes the request
it takes the data and the fieldnames inside these properties and inserts them
accordingly.

Please do let me know if i have missed something.

VN:F [1.8.1_1037]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.1_1037]
Rating: 0 (from 0 votes)
Share and Enjoy:
  • Digg
  • Google Bookmarks
  • del.icio.us
  • Furl
  • LinkedIn
  • Live
  • Reddit
  • StumbleUpon
  • TwitThis
  • Yahoo! Buzz
  • YahooMyWeb

Comments (2)

Using Dynamic Entity in MS CRM 4.0

The other day, I was helping my wife with one of the issues she was facing in MS CRM 4.0. The code was basically trying to get an instance to the running crmwebservice and use that instance to create a contact or a update a contact in MS CRM.  The issue we where facing in particular, was a type casting issue where an implicit cast of PropertyCollection to Property was not being done. After some research here and here. We ended up with the following code

StringProperty sp_Field1 = new StringProperty("Field1","Value1");
StringProperty sp_Field2 = new StringProperty("Field2","Value1");
 
// Create the DynamicEntity object.
DynamicEntity contactEntity = new DynamicEntity();
// Set the name of the entity type.
contactEntity.Name = EntityName.contact.ToString();
 
// Set the properties of the contact.
contactEntity.Properties.Add(sp_Field1);
contactEntity.Properties.Add(sp_Field);

Thanks to SaaS Developer and Jude Lee for their help in resolving this issue.

Please note, the above code does not have the full code, like getting the crmwebservice object. That part is for another post. One other thing of importance is there are two PropertyCollection classes, one belongs to the System.Data namespace and other one belongs to Microsoft.CRM.SDK namespace. So when you are declaring an object of PropertyCollection type, ensure you prefix the namespace accordingly.

VN:F [1.8.1_1037]
Rating: 3.0/10 (1 vote cast)
VN:F [1.8.1_1037]
Rating: 0 (from 0 votes)
Share and Enjoy:
  • Digg
  • Google Bookmarks
  • del.icio.us
  • Furl
  • LinkedIn
  • Live
  • Reddit
  • StumbleUpon
  • TwitThis
  • Yahoo! Buzz
  • YahooMyWeb

Leave a Comment