Search code examples
sitecoresitecore7

Sitecore Delete the most newly added child


In sitecore I want to delete the newly added child.

item.DeleteChildren(); 

Delete all the children under item But I want to delete the most updated child or the newly added child.


Solution

  • I would just loop through the items and see which one was created most recently. Something like this:

    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();
    }
    

    I've used Item.Statistics.Created here, but Item.Statistics.Updated is also available