且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Sitecore的快速查询 - 如何搜索包含特殊字符(如撇号)的文本?

更新时间:2022-10-23 20:49:16

这是一个后做你想要做什么。基本上,你必须使用一个转义双引号来包装包含引号的字段值。

  VAR搜索关键词=热N'好吃;
VAR的查询=的String.Format(快:/ Sitecore的/内容/首页/产品展示// * [@ ContentDescriptionText = \%{0}%\],搜索关键词);
变种项= Database.SelectItems(查询);
 

编辑:在逃避快速查询,添加链接CoolMcGrr引用和它的局限性。这里

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.

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