I'm using SPServices to query a list, using CAML, and the column I'm searching (Title) have values that contains < and >.

Is there anyway I can use CAML to find these values? It seems that I'm getting undefined returned. If the string doesn't contain these characters, I get exactly what I want back.

Using U2U CAML Query Builder, it returns this CAML:

<Query><Where><Eq><FieldRef Name="Title" /><Value Type="Text">Value &lt; 10</Value></Eq></Where></Query>

where the value of title is actually Value < 10.

Thank you very much,


Here is my solution that worked for me.

var a = "Test & < > '10'"; 

var newA = a.replace(/&/g, "&amp;").replace(/</g,"&lt;").replace(/>/g, "&gt;").replace(/'/g,"&#039;").replace(/"/g, "&quot;");

Then I pass in newA to my SPServices call where I'm using CAML to find my data.

CAMLQuery: "<Query><Where><And><Eq><FieldRef Name='title'/><Value Type='Text'>" + newA + "</Value></Eq>" ....
  • I'm trying something like this: var a = "Value < 10"; var newA = a.replace(/&/g, "&amp;").replace(/</g,"&lt;").replace(/>/g, "&gt;").replace(/'/g,"&#039;").replace(/"/g, "&quot;"); Where I will pass in newA to the CAML query.
    – DrZ
    Apr 22, 2011 at 15:23
  • That seemed to have worked. I know my strings will contain other characters like & < > " ' so I handled that as well.
    – DrZ
    Apr 22, 2011 at 15:34
  • That question is about getting the value not setting it.
    – user3470
    Nov 30, 2011 at 8:49
  • Indeed, but the solution to the illegal character problem is the same.
    – Stu Pegg
    Nov 30, 2011 at 9:40
  • If i replace the characters like in the solution of that question they will be displayed as is like i noted in my question.
    – user3470
    Nov 30, 2011 at 9:47

6 Answers 6


My advice would be to use the CDATA escape, rather than replacing the offending characters one by one:

<Query><Where><Eq><FieldRef Name="Title" /><Value Type="Text"><![CDATA[Value < 10]]></Value></Eq></Where></Query>

That should cure most XML special character ills.

  • The CDATA option does work.
    – DrZ
    Apr 25, 2011 at 11:54
  • @DrZ: Good good.
    – Stu Pegg
    Apr 25, 2011 at 18:22
  • I like that. That's an elegant solution.
    – Ben_Coding
    Jun 19, 2013 at 15:24
  • does it work with ( and ) ?
    – edelwater
    Apr 3, 2015 at 15:46
  • @edelwater It should work with almost anything.
    – Stu Pegg
    Apr 3, 2015 at 19:19

You need to convert the special characters, see an example of code here: https://devspoint.wordpress.com/2011/01/07/two-quick-javascript-snippets-i-use-everyday/#comments

SharePoint actually has a built-in function for that, called STSHtmlEncode (cf. my comment in the above link).


SharePoint encodes the html before its stored in the field to avoid XSS.

If you want to decode the html in code use SPEncode.HtmlDecode(item["Title"]). Or you can also use Jquery for it (to decode on the View Item page) : http://www.prodevtips.com/2008/10/21/jquery-plugin-html-decode-and-encode/

  • Thank you for your answer i dont have trouble decoding the information. If i create a caml query with decoded information the result will be applied as is.
    – user3470
    Nov 30, 2011 at 9:40

I am not sure I fully get what you are doing, but you might try using the Contains option in lieu of the Eq option. The "<" is just formatted so that you have clean XML. The Contains option will find all items that contain the selected option:

<Contains><FieldRef Name='Title'/><Value Type='Text'>&lt;</Value></Contains>
  • The value I'm specifically looking for is: Value < 10. Not just any string that has a < in it. The < is actually part of the overall value I'm looking for. Thank you for your assistance.
    – DrZ
    Apr 22, 2011 at 14:25

Ok guys i found the solution.

If i want to update a field with data that contains illegal XML characters using CAML query i need to put these characters within a <![CDATA[]]> element.

In the Title example above the code will be like this:

<Field Name=\"Title\"><![CDATA[<title>]]></Field>

Thank you all.

<Query><Where><BeginsWith><FieldRef Name="Title" /><Value Type="Text">&lt;</Value></BeginsWith></Where><OrderBy><FieldRef Name="Title" Ascending="True" /></OrderBy></Query>

its an eg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.