10.4
  1. Access items related to another item

Access the children of an item

You can use the Sitecore.Data.Items.Children property to access the children of an item. For example, to access the children of the /Sitecore/Content/Home item in the Master database:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

foreach(Sitecore.Data.Items.Item child in home.Children)
{
    // Process child item
}

The system creates a new Sitecore.Collections.Childlist object each time you access the Sitecore.Data.Items.Item.Children property. The implementation of the C# for each statement accesses the property only once.

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

Sitecore.Collections.ChildList children = home.Children;

for(int childIndex=0; childIndex<children.Count ; childIndex++)
{
    // Process child item
}

You can also get the children items by invoking the items GetChildren method. For example, to retrieve children items of the current page, use this code:

var children = currentPage.GetChildren();

If you have suggestions for improving this article, let us know!