Question

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>" ....
Was it helpful?

Solution

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.

OTHER TIPS

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

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>

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/

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

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top