Querying a PartyList field

CRM doesn’t allow a PartyList field to be queried using a QueryExpression (it doesn’t allow it using Advanced Find either). A colleague had tried to query the Campaign Response records to ascertain whether a Contact had responded to a Campaign before before performing some other process(es).

The trick was to query the Activity Pointer entity (where the regardingobjectid is the Campaign) and then perform a link to the Activity Party entity (where the party is the Contact). The following function shows this:

private static bool AlreadyResponded(string strCampaignId, string strCustomerId)
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "regardingobjectid";
condition.Operator = ConditionOperator.Equal;
condition.Values = new object[] { strCampaignId };
FilterExpression filter = new FilterExpression();
filter.Conditions = new ConditionExpression[] { condition };
filter.FilterOperator = LogicalOperator.And;
# region link to the Activity Party
entityLinkEntity link = new LinkEntity();
link.LinkFromEntityName = EntityName.activitypointer.ToString();
link.LinkToEntityName = EntityName.activityparty.ToString();
link.LinkFromAttributeName = "activityid";
link.LinkToAttributeName = "activityid";
ConditionExpression linkCondition = new ConditionExpression();
linkCondition.AttributeName = "partyid";
linkCondition.Operator = ConditionOperator.Equal;
linkCondition.Values = new object[] { strCustomerId };
FilterExpression linkFilter = new FilterExpression();
linkFilter.Conditions = new ConditionExpression[] { linkCondition };
linkFilter.FilterOperator = LogicalOperator.And;
link.LinkCriteria = linkFilter;
# endregion
QueryExpression query = new QueryExpression();
query.ColumnSet = new AllColumns();
query.Criteria = filter;
query.EntityName = EntityName.activitypointer.ToString();
query.LinkEntities = new LinkEntity[] { link };
BusinessEntityCollection collection = crm.RetrieveMultiple(query);
if (collection != null && collection.BusinessEntities.Length > 0)
return true;
return false;