Sitecore Fast Query - How to search for text conta

2019-06-24 08:06发布

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.

2条回答
虎瘦雄心在
2楼-- · 2019-06-24 08:41

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

查看更多
▲ chillily
3楼-- · 2019-06-24 08:43

Based on @Kevin's answer and link, I was able to come up with a suitable solution:

var searchTerm = "hot n' tasty";
// old code for comparison
//var query = string.Format("fast:/sitecore/content/Home/Products//*[@ContentDescriptionText='%{0}%']", searchTerm);
var query = string.Format("fast:/sitecore/content/Home/Products//*[@ContentDescriptionText=\"%{0}%\"]", searchTerm);
var items = Database.SelectItems(query);
查看更多
登录 后发表回答