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);
}

System.Security.SecurityException in CRM Plugin

I recently got the following error when trying to call the Bing Maps rest endpoint in a CRM 2011 plug-in. Since I wasn’t able to reproduce this in my test code, I did a search, and found out that calls to HttpUtility are not allowed in the sandbox environment for plug-ins for CRM Online.
I had the following piece of code:
HttpUtility.UrlEncode(...)
I changed it to:
Uri.EscapeUriString(...)
This resolved the issue, and now I am geocoding with no problems.