Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Remko 118 posts 283 karma points
    Mar 12, 2019 @ 15:39
    Remko
    0

    V8 - IContent get Children?

    With ContentService it was alway easy to get a node as IContent and get it's Children too. Children (or GetChildren()) is gone now. What's the best way to get all Children of a certain node when you have an IContent object?

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Mar 12, 2019 @ 17:23
    Marc Goodson
    103

    Hi Remko

    The ContentService has a few methods that can help you out

    https://github.com/umbraco/Umbraco-CMS/blob/bf28dfe04aa2b4b9efdd83f9d5566e97954f9fba/src/Umbraco.Core/Services/Implement/ContentService.cs#L565

    There is a GetPagedChildren method that will return an IEnumerable of IContent items, based on the Id of a content item:

    var contentId = 1234;
    //is there children for this content id?
    bool hasChildren = _contentService.HasChildren(contentId);
    
    //get the first 100 children... and has an out variable giving you the total count
    long totalChildren;
    IEnumerable<IContent> children = _contentService.GetPagedChildren(contentId, 1, 100, out totalChildren);
    
    //there is also a seperate ChildCount method
    
    var childCount = _contentService.ChildCount(contentId)
    

    regards

    Marc

  • Magnus Eriksson 122 posts 362 karma points
    May 15, 2019 @ 14:09
    Magnus Eriksson
    1

    With dependency injection, you can look at Sebastiaans posts in this thread for examples on how to do this: https://our.umbraco.com/forum/umbraco-8/96270-using-umbracohelper-in-a-custom-class-in-v8#comment-304631

    Regards, Magnus

  • Alexander Gräf 25 posts 131 karma points
    Dec 10, 2020 @ 12:17
    Alexander Gräf
    2

    Just a heads-up that the _contentService.GetPagedChildren pageIndex parameter starts at 0, so you'd technically be fetching the second page already.

  • Remko 118 posts 283 karma points
    Mar 13, 2019 @ 15:10
    Remko
    0

    Hi Marc,

    Thank you.

    I found the GetPagedChildren, but thought there should be any .Children() entension method.. But guess I will then have to use this one.

Please Sign in or register to post replies

Write your reply to:

Draft