Tuesday, January 10, 2012

Add an ‘Active’ Filter to Your QueryExpression

Here’s a generic snippet of code to add a QueryExpression ‘Active’ filter (or another specified).  It will work on almost any CRM Entity.

private void AddActiveCondition(QueryExpression query, string stateCode= "Active")
{
ConditionExpression condition1
= new ConditionExpression();
condition1.AttributeName
= "statecode";
condition1.Operator
= ConditionOperator.Equal;
condition1.Values.Add(stateCode);
if(query.Criteria == null)
query.Criteria
= new FilterExpression { FilterOperator = LogicalOperator.And };
query.Criteria.Conditions.Add(condition1);
}

No comments: