1

Is there any possible to return a item children as tree? I tried to use GetDescendants however, all items are returned at the same level as a list.

Example:

-FORM
    ITEM1
    ITEM2
        ITEM2.1
        ITEM2.2
              ITEM2.2.1
        ITEM2.3
    ITEM3
        ITEM3.1
3
  • 2
    Use foreach of item.getchildren() to get this results Commented Mar 18, 2021 at 19:58
  • Do you want to get all Descendants of the specific item as Tree in objects? Commented Mar 18, 2021 at 20:41
  • Did you check Glass Mapper possibilities for your purpose? It can map items with children's. sitecore.stackexchange.com/questions/16553/… Commented Mar 18, 2021 at 20:44

1 Answer 1

1

Depending on where you want to display the items and if you aren't iterating the whole sitecore structure you can get the child items recursively:

public static Result ChildrenRecursive(Item item)
{
    var r = new Result();
    var itemChildren = item.Children;
    r.Items.AddRange(itemChildren);
    foreach (Item i in itemChildren)
    {
        if (i.HasChildren)
        {
            r.Items.AddRange(ChildrenRecursive(i).Items);
        }
    }
    return r;
}

You would need to modify the above code to suit your needs, for example if you need to have a tree object you would need a custom object with subitems properties in order to simulate the sitecore tree.

If you need to read over 100 items I would suggest building a solution using content search and setup indexing for your items.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.