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

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 that you have read and understand our privacy policy and code of conduct.

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