Sitecore删除最新添加的子项

时间:2015-06-25 20:03:28

标签: sitecore sitecore7

在sitecore中我想删除新添加的孩子。

item.DeleteChildren(); 

删除item下的所有子项但我想删除最新更新的子项或新添加的子项。

2 个答案:

答案 0 :(得分:4)

我会循环浏览这些项目并查看最近创建的项目。像这样:

Item newestItem = null;
foreach(Item child in parent.Children)
{
    if (newestItem == null || child.Statistics.Created > newestItem.Statistics.Created)
    {
        newestItem = child;
    }
}

if (newestItem != null)
{
    newestItem.Delete();
}

我在这里使用了Item.Statistics.Created,但Item.Statistics.Updated也可用

答案 1 :(得分:2)

您也可以使用Linq:

var newestItem = item.Children.OrderByDescending(child => child.Statistics.Created).FirstOrDefault();

If (newestItem != null)
    newestItem.Delete();
相关问题