Thursday, February 23, 2012

MS CRM Retrieve and RetrieveMultiple methods do not work on Custom or Dynamic Entities

RetrieveMultiple do not work to retrieve data from custom entities and it ends with the runtime exception if we try to get the records using this method.
Exception: Error in XML document. The specified type was not recognized.

This is because RetrieveMultiple works on strongly typed entities only. When we make use of SDK's we return custom entities as dynamic entities which are not strongly typed. In order to retrieve the Dynamic Entity, we should use Execute method as shown in the below code.

RetrieveMultipleRequest req = new RetrieveMultipleRequest();
req.Query = queryExpression;
req.ReturnDynamicEntities = true;
RetrieveMultipleResponse resp = (RetrieveMultipleResponse)_service.Execute(req);
if (resp != null && resp.BusinessEntityCollection.BusinessEntities.Count > 0)
{
  Guid columnID=Guid.Empty;
  dynEntity = (DynamicEntity)resp.BusinessEntityCollection.BusinessEntities[0];
  if ( dynEntity.Properties.Contains("new_columnid"))
     columnID = ((Lookup)dynEntity.Properties[" new_columnid"]).Value;
}

If we use crm webservices instead of sdk, we can make use of RetrieveMultiple without any issues.

No comments:

Post a Comment