sitecore - 从另一个分支获取 Sitecore sibling

标签 sitecore sitecore6

我们在 sitecore 中有一个数据结构,它具有相同“深度”的相同模板。我们正在制作具有以下结构的单元、类(class)和事件的类内容:

Unit 1
-- Lesson 1
---- Activity 1
---- Activity 2
-- Lesson 2
---- Activity 3
---- Activity 4
Unit 2
-- Lesson 3
---- Activity 5
---- Activity 6
-- Lesson 4
---- Activity 7
---- Activity 8

等等。当我在一个 activity 项目上时,我想返回该特定 unit 中的下一个 activity 项目,如果没有更多则返回 null该单位的事件。

到目前为止我能做的最好的事情是针对当前事件 unit 祖先(很容易找到)并获取它下面的所有 activities ,然后循环通过所有这些以获得上一个/下一个事件。似乎必须有更好的方法来实现这一点,所以我想我会把它扔在这里征求意见。

当前代码

Item unit = Sitecore.Context.Item.Axes.SelectSingleItem("ancestor-or-self::*[@@templatename='Unit']");
Item[] allActivities = unit.Database.SelectItems("ancestor-or-self::*[@@templatename='Activity']");

foreach(Item thisitem in allActivities){
    //Process here
}

sibling ("Following"和 "Preceding")不起作用,因为它只返回同一 lesson 下的直接 sibling ,而不是根据需要返回 unit

最佳答案

我认为你的想法是正确的。需要注意的几点:

  1. unit.Database.SelectItems() 从单元数据库的根开始,不使用单元作为起始上下文。如果您打算向下遍历以获取该单元的所有事件,则需要使用 unit.Axes.SelectItems()

  2. 根据每个单元的事件项目数,您可能需要考虑使用 sitecore 快速查询或可能的 Lucene 来处理选择。

这是一个如何处理上一个/下一个逻辑的例子。如果/当前一个或下一个兄弟不可用时,使用 .FirstOrDefault() 将返回 null。

Item unit = Sitecore.Context.Item.Axes.SelectSingleItem("ancestor-or-self::*[@@templatename='Unit']");
Item[] unitActivities = unit.Axes.SelectItems("descendant::*[@@templatename='Activity']");
// The order of 'unitActivities' defaults to the order that items appear in Sitecore tree.
// Perform additional sorting here if needed

var nextActivity = unitActivities.SkipWhile(i => i.ID != Sitecore.Context.Item.ID).Skip(1).FirstOrDefault();
var prevActivity = unitActivities.Reverse().SkipWhile(i => i.ID != Sitecore.Context.Item.ID).Skip(1).FirstOrDefault();

关于sitecore - 从另一个分支获取 Sitecore sibling ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11961684/

相关文章:

xpath - Sitecore xpath 查询不起作用

Sitecore - 富文本编辑器 - 使用类属性创建新段落

search - 有没有办法防止使用 Sitecore 搜索和 Lucene 进行部分单词匹配?

indexing - Sitecore Lucene 索引 - 按模板排除项目

azure - Sitecore 9 - 负载测试结果出现 502 错误

web-services - Sitecore:从 Field 值以编程方式解析 Url

sitecore - 打开 Sitecore 媒体库时出现 404

sitecore - 使工作箱显示 sitecore/system/workflows 之外的工作流程

c# - Sitecore Scheduled Tasks 与 Windows Task Scheduler 数据迁移

sitecore - 在 Sitecore 中设置从电子邮件地址忘记的密码