Surendra Sharma

Surendra Sharma

Search This Blog

Monday, March 16, 2015

How to programmatically iterate all the items of content tree in Sitecore

Do you want to iterate through all the items of Content tree in Sitecore using code?

Here is a way to do it. Just put Home item ID or any start item ID

var FirstItem = Sitecore.Context.Database.GetItem(new ID("{A00A11B6-E6DB-45AB-8B54-636FEC3B5523}")) ;
PrintName(FirstItem , 1);

Rest of the items should be iterate by following recursive function

private Item PrintName(Item mainItem, int icounter)
{
    string f = new string('-', icounter);
    TextBox1.Text += f + mainItem.DisplayName + System.Environment.NewLine;

    if (mainItem.HasChildren)
    {
        icounter++;
        foreach (var myInnerItem in mainItem.Children.ToList())
        {
            PrintName(myInnerItem, icounter);
        }
    }

    icounter--;
    return null;
}


Please leave your comments or share this code if it’s useful for you.

No comments:

Post a Comment