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

1
2
3
4
5
6
7
8
9
10
11
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_Field2);

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.

Leave a Reply

Your email address will not be published. Required fields are marked *