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,

dave


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>" ....
link|improve this question

75% accept rate
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 '11 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 '11 at 15:34
Maybe post your solution in an answer and accept it? – Kit Menke Apr 22 '11 at 18:28
That question is about getting the value not setting it. – Moussa Nov 30 '11 at 8:49
Indeed, but the solution to the illegal character problem is the same. – Stuart Pegg Nov 30 '11 at 9:40
show 4 more comments
feedback

6 Answers

up vote 2 down vote accepted

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).

link|improve this answer
Thank you. The STSHtmlEncode function works great. – DrZ Apr 25 '11 at 11:54
feedback

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.

link|improve this answer
The CDATA option does work. – DrZ Apr 25 '11 at 11:54
@DrZ: Good good. – Stuart Pegg Apr 25 '11 at 18:22
feedback

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>
link|improve this answer
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 '11 at 14:25
feedback

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/

link|improve this answer
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. – Moussa Nov 30 '11 at 9:40
feedback
<Query><Where><BeginsWith><FieldRef Name="Title" /><Value Type="Text">&lt;</Value></BeginsWith></Where><OrderBy><FieldRef Name="Title" Ascending="True" /></OrderBy></Query>

its an eg

link|improve this answer
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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