Special Characters in Fetch XML


We all know that Fetch XML is the wonderful and great feature given in CRM. 
Today, I got an error while working on retrieving some records from CRM. The issue was caused by the special characters.

Here is the my C# code in which I am retrieving account information by passing name.

public static Entity GetAccountInformation1(IOrganizationService objService)

     Entity entAccount = null;
     string strAccountName = “johnson & johnson”;
     string strFetchXML = @”
                                <condition attribute='name' operator='eq' value='" + strAccountName + “‘/>” +
      EntityCollection entCollection = objService.RetrieveMultiple(new FetchExpression(strFetchXML));
      if (entCollection != null && entCollection.Entities.Count > 0)
            entAccount = entCollection.Entities[0];
      return entAccount;

And after I execute, got the below exception.

Don’t worry, this is very simple to solve. Just use HTMLEncode before passing the dynamic values.
Here is the thing what I have changed for making the code to work.

Hope it helps.

Happy CRM’ing


One thought on “Special Characters in Fetch XML

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s