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

1
2
3
4
5
6
7
8
9
10
//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; <span id="more-19"></span>
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.

Leave a Reply

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