Thursday, May 7, 2009

Retrieve Entity field value

This function used to return field value inside plugin,

public static object GetEntityFieldValue(IPluginExecutionContext context,string entityName, Guid instanceId,string fieldName){
ICrmService pservice = context.CreateCrmService(false);
TargetRetrieveDynamic targetRetrieve = new TargetRetrieveDynamic();
targetRetrieve.EntityName = entityName;
targetRetrieve.EntityId = instanceId;
RetrieveRequest retrieve = new RetrieveRequest();
retrieve.Target = targetRetrieve;
retrieve.ColumnSet = new AllColumns();
retrieve.ReturnDynamicEntities = true;
RetrieveResponse retrieved = (RetrieveResponse)pservice.Execute(retrieve);
DynamicEntity entity = (DynamicEntity)retrieved.BusinessEntity;
return entity.Properties[fieldName];
}

Enjoy,
Rami Heleg