Monday, 18 May 2015

Special Characters in Fetch XML

Hi,

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 = @"<fetch version='1.0' output-format='xml-platform'  mapping='logical' distinct='false'>
                            <entity name='account'>
                            <attribute name='name' />
                            <attribute name='primarycontactid' />
                            <attribute name='telephone1' />
                            <attribute name='accountid' />
                            <order attribute='name' descending='false' />
                            <filter type='and'>
                                <condition attribute='name' operator='eq' value='" + strAccountName + "'/>" +
                             @"</filter>
                             </entity>
                             </fetch>";
      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
Gopinath.

 

1 comment: