Crm 2011 get custom entity record attribute without early bound types

Nozim picture Nozim · Sep 9, 2011 · Viewed 14.3k times · Source


I'm searching for a method of retrieving the custom entity attribute without generating early bind types with crmsvcutil.

Is there any solution for my problem?

Answer

Luke Baulch picture Luke Baulch · Sep 12, 2011

You don't need to generate early bound types to retrieve entity data from CRM. You can work with a type called Entity (which is similar to the DynamicEntity of CRM4).

The SDK has some examples about how to work with late bound entities here.

This entity class is not strongly typed (unlike early-bound entities generated from crmsvcutil) so you have to perform casts yourself. There is a method on Entity which will help with this. The following code might give you some idea about how to retrieve a late-bound entity.

IOrganizationService service = GetOrganizationService();
var entity = service.Retrieve(entityName,
                                entityId,
                                new ColumnSet(new[]
                                                {
                                                    stringAttributeName,
                                                    intAttributeName,
                                                    floatAttributeName,
                                                    boolAttributeName,
                                                    optionSetAttributeName,
                                                    entityReferenceAttributeName,
                                                }));
var stringValue = entity.GetAttributeValue<string>(stringAttributeName);
var intValue = entity.GetAttributeValue<int?>(intAttributeName);
var floatValue = entity.GetAttributeValue<double?>(floatAttributeName);
var boolValue = entity.GetAttributeValue<bool?>(boolAttributeName);
var optionSetValue = entity.GetAttributeValue<OptionSetValue>(optionSetAttributeName);
var entityReferenceValue = entity.GetAttributeValue<EntityReference>(entityReferenceAttributeName);