Search code examples
c#sitecoresitecore7

Sitecore Children of Datasource


I am applying a datasource to a sublayout and obtaining the values of its children as follows:

Sitecore.Collections.ChildList childItems;

if (Sitecore.Context.Database.GetItem(this.DataSource) != null)
{
    childItems = Sitecore.Context.Database.GetItem(this.DataSource).GetChildren();
}
else
{
    litDataSourceError.Text += "You need to set a datasource";
}

foreach (Item item in childItems)
{
    litDataSourceError.Text += "<h2>" + item.Fields["Title"].Value + "</h2>";
}

This is working as expected however these items also have children which I would like to output.

So my question is how to look down a further node within my ForEach to obtain the Childrens Children - there will only be these 2 levels of structure.


Solution

  • You should do the same as you did for your datasource (fetch the children of the Sitecore Item):

    foreach (Item item in childItems)
    {
        litDataSourceError.Text += "<h2>" + item.Fields["Title"].Value + "</h2>";
        foreach (Item child in item.GetChildren())
        {
            ...
        }
    }