3

我们在 sitecore 中有一个数据结构,它具有相同“深度”的相同模板。我们正在制作具有以下结构的单元、课程和活动的课堂内容:

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项目上时,我想返回该activity特定项目中的下一个项目unit,如果该单元中没有更多活动,则返回 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
}

Siblings ("Following" & "Preceeding") 不起作用,因为它只返回相同的直接兄弟姐妹lesson,而不是unit根据需要返回。

4

1 回答 1

3

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

  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();
于 2012-08-14T23:19:58.047 回答