Access the children of an item
Version: 10.4
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:
RequestResponse
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.
RequestResponse
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();