Вопрос

I'm getting error when I try to execute the last line in this reply. I run the following code:

QueryExpression query = new QueryExpression
{
  LogicalName = "contact",
  ...
}

BusinessEntityCollection response = ServiceProxy.RetrieveMultiple(query);
Logify("count: " + response.BusinessEntities.Count);
BusinessEntity piff= response.BusinessEntities.First();
Logify("piff: " + (piff != null));

DynamicEntity poof = response.BusinessEntities.First() as DynamicEntity;
Logify("poof: " + (poof != null));

The count is 1, so the call is performed correctly and yields something. According to the log, piff isn't null, so it contains something. However, after the conversion (which is required in order to access the fields of the entity), I get it to be null (or I get an Exception when trying to explicitly cast the shabang).

The exception is:

Unable to cast object of type 'Microsoft.Crm.SdkTypeProxy.contact' to type 'Microsoft.Crm.Sdk.DynamicEntity'.

What to do?!

Это было полезно?

Решение

The answer is to use RetrieveMultipleRequest. Reworked code:

QueryExpression query = new QueryExpression
{
    LogicalName = "contact",
    ...
}

RetrieveMultipleRequest rmr = new RetrieveMultipleRequest()
{
    Query = query,
    ReturnDynamicEntities = true
};

RetrieveMultipleResponse rmrresp = ServiceProxy.Execute(rmr) as RetrieveMultipleResponse;
BusinessEntityCollection response = rmrresp.BusinessEntityCollection;
Logify("count: " + response.BusinessEntities.Count);
BusinessEntity piff= response.BusinessEntities.First();
Logify("piff: " + (piff != null));

DynamicEntity poof = response.BusinessEntities.First() as DynamicEntity;
Logify("poof: " + (poof != null));
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top