Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sitecore Fast Query - How to search for text containing special characters (such as an apostrophe)?

Perhaps I'm blind but I can't find any documentation on escaping special characters for Sitecore's fast query.

For example, I want to search content items that contain the following text in their description: "hot n' tasty"

Note: I've tried the following escaping:

var searchTerm = "hot n'' tasty";  // thought it might need to be escaped for sql
var searchTerm = "#hot n' tasty#"; // this is how you do some other sitecore escaping
var searchTerm = "hot n\' tasty";

Sample application code:

var searchTerm = "hot n' tasty";
var query = string.Format("fast:/sitecore/content/Home/Products//*[@ContentDescriptionText='%{0}%']", searchTerm);
var items = Database.SelectItems(query);

Currently, this is giving me an exception so I'm assuming I need to do some sort of escaping:

"]" expected at position 67.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Sitecore.Data.Query.ParseException: "]" expected at position 67.

like image 831
longda Avatar asked Dec 16 '22 10:12

longda


1 Answers

Here is a post doing exactly what you are trying to do. Basically you have to use an escaped double quote to wrap the field value that contains a quote.

var searchTerm = "hot n' tasty";
var query = string.Format("fast:/sitecore/content/Home/Products//*[@ContentDescriptionText=\"%{0}%\"]", searchTerm);
var items = Database.SelectItems(query);

EDIT: Added link CoolMcGrr references on escaping fast queries and it's limitations.. here

like image 55
Kevin Avatar answered Dec 28 '22 09:12

Kevin