error CAML Query containing special characters

Posted on : Fri, 15th May 15 03:10 pm UTC
100+ votes

If you are building a CAML Query to match a value which contains a special character like & (say SharePoint&InfoPath), you will receive an error and query will fail.

Resolution - Handle this the way you handle special characters in XML.
Include your value within < ![CDATA[ value ]] >.

Incorrect (throws error) -
<Eq><FieldRef Name="Title" /><Value Type="Text">SharePoint&InfoPath</Value></Eq>
Correct -
<Eq><FieldRef Name="Title" /><Value Type="Text"><![CDATA[SharePoint&InfoPath]]></Value></Eq>

In real scenarios, build your CAML Query by appending the value in the query, like –
<Eq><FieldRef Name="Title" /><Value Type="Text"><![CDATA[‘ + value + ‘]]> </Value></Eq>
[where value = text to be matched]

Code2care's mission is to share varied knowledge in technical and non-technical areas gathered during day-to-day learnings and development activities so that our visitors can leverage this portal to find solutions to their queries without re-inventing the wheel. Technical posts include Learnings, Tutorials, Video Tutorials, Code Snippets, Tips-n-tricks.

Follow us : Facebook - Twitter - Google +