I use JS to create a CAML query. When it is generated, it looks like this:

@v1 = {"ViewXml":"<View Scope='RecursiveAll'><ViewFields><FieldRef Name='ID'/><FieldRef Name='Title'/><FieldRef
 Name='Designation'/><FieldRef Name='srd_title2'/></ViewFields><Query><Where><Contains><FieldRef Name
='Subject'/>My Value Here<Value Type='Note'>

When users attempt to search using values such as "My Hand\Number2" or "Body & Styles", I receive errors such as "An unrecognized escape sequence '\' was found in a JSON string value". I changed the code to do a manual replacement (e.g., param.replace("\\","\\\\"), and that worked for back slashes. But is there a good way to make sure I'm checking for all of these? I tried using CDATA such as in this example: https://sharepoint.stackexchange.com/a/11703/24039 to no avail.

update: I actually used val.(/\\/g, "\\\\") as Wrapping "val" in encodeURI or encodeURIComponent is not working for this case. I didn't realize the double \ became escaped when I submitted the question, so I wrapped it in a code block now. Sorry for the confusion.

2 Answers 2


I recommend using the build-in JS functions of SharePoint to work with these kind of errors. Try using escapeProperly()

Here is a page that lists some of those functions: https://praneethmoka.wordpress.com/2012/01/12/some-useful-javascript-variablesfunctions-in-sharepoint/

  • Thank you. Please see my updated question and my response to @ECM4D 's answer.
    – Dinerdo
    Commented Sep 3, 2015 at 13:36

Usually in SharePoint 2013 REST special characters are escaped using HTML encoding in query string. If you are using JavaScript try using encodeURIComponent method to format your CAML query variable value.

More about it: http://www.sharepointyammertech.com/2014/12/sharepoint-2013-how-to-handle-special.html

  • I've done this and it doesn't work. The issue is that it needs to be printed "as escaped." since that's the only way SP will accept it in CAML. I made a mistake in my original question - I had to use val.(/\\/g, "\\\\"). Does this help my question wording?
    – Dinerdo
    Commented Sep 3, 2015 at 13:36

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.