Search code examples
c#dynamics-crmdynamics-crm-2015

RetrieveMultiple using type parameter doesn't work


For some reason, the following function works:

public EntityCollection RetrieveMultipleByIds(IOrganizationService service, string entityLogicalName, string[] columnSet, string idFieldName, string[] guids)
{
    QueryExpression query = new QueryExpression(entityLogicalName);
    query.ColumnSet = new ColumnSet(columnSet);
    query.Criteria = new FilterExpression();
    query.Criteria.AddCondition(idFieldName, ConditionOperator.In, guids);
    EntityCollection entityCollection = service.RetrieveMultiple(query);
    return entityCollection;
}

But this one - using type parameter - doesn't work:

public EntityCollection RetrieveMultipleByIds<T>(IOrganizationService service, string entityLogicalName, string[] columnSet, string idFieldName, T[] guids)
{
     QueryExpression query = new QueryExpression(entityLogicalName);
     query.ColumnSet = new ColumnSet(columnSet);
     query.Criteria = new FilterExpression();
     query.Criteria.AddCondition(idFieldName, ConditionOperator.In, guids);
     EntityCollection entityCollection = service.RetrieveMultiple(query);
     return entityCollection;
}

Both are called in the same way (Note that the literal-string guid is just for this example, and you can be assured the id i'm passing exists):

EntityCollection entityCollection =  
RetrieveMultipleByIds("org_session", new string[] { "org_sessionname" }, "org_sessionid", new string[] {"73A5794E-1662-E711-80FF-005056B74623"});

And the exception thrown is:

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://schemas.microsoft.com/xrm/2011/Contracts/Services:query. The InnerException message was 'Error in line 1 position 2826. Element 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:anyType' contains data from a type that maps to the name 'System:String[]'. The deserializer has no knowledge of any type that maps to this name. Consider changing the implementation of the ResolveName method on your DataContractResolver to return a non-null value for name 'String[]' and namespace 'System'.'. Please see InnerException for more details.

I know there are similiar questions asking about this kind of exception, but none is related to using type parameters. If someone stumbled into the same issue and managed to find the problem and solve it, I'd be glad to hear about it.


Solution

  • In query.Criteria.AddCondition(idFieldName, ConditionOperator.In, guids); argument guids cannot be a generic type. It's just a serialization issue. To go safe only arrays of type string or object should be allowed in the ConditionExpression.