在sitecore中使用快速查询获取所有子项和子项

Son*_*oni 4 sitecore sitecore7

在sitecore我想得到继承形式"工业产品模板"的所有孩子和大孩子.

下面是快速查询,但它给出错误错误:预期在第5位的字符串结束.

快速查询:

_masterdb.SelectItems("query:/sitecore/content/Product Catalog/Industrial/Products/*[@@templatename='Industrial Product']")

小智 6

这不是快速查询,您使用的是普通查询.请使用以下内容:

Sitecore.Data.Items.Item[] items = 
 database.SelectItems("fast:/sitecore/content/Product Catalog/Industrial/Products//*[@@templateid='yourTemplateId']"); 
Run Code Online (Sandbox Code Playgroud)

另外请使用@@ templateid而不是@@ templatename,我做了一些测试,使用@@ templateid更快.

也可以看看这里关于使用FastQuery.


Kev*_*ühl 6

您的查询不是快速查询,如果您使用以下查询,它对我有用:

_masterdb.SelectItems("fast:/sitecore/content/Product Catalog/Industrial/Products//*[@@templatename='Industrial Product']");
Run Code Online (Sandbox Code Playgroud)

此查询中有两处更改:

  • query开头的关键字替换为fast.这意味着它不是"普通"Sitecore查询,它成为Sitecore快速查询.有关使用快速查询的更多信息,请阅读本指南.
  • 在选择项目之前*[@@templatename='Industrial Product'],我添加了一个双斜杠//.这意味着,它使用您的模板对所有项目进行递归搜索.使用您的查询,您只搜索直接子项.

另外,我建议你使用关键字@@templateid代替@@templatename,因为sitecore登山者说它更快,如果你重命名模板也没问题.所以你的查询最后看起来像这样:

fast:/sitecore/content/Product Catalog/Industrial/Products//*[@@templateid='{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}']
Run Code Online (Sandbox Code Playgroud)

注意:这不仅选择您的孩子和大孩子.这会以递归方式为您提供包含此模板的所有项目.