Copied to clipboard

Flag this post as spam?

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


  • ozb3n 4 posts 34 karma points
    Jul 20, 2015 @ 04:09
    ozb3n
    0

    Getting published children of current page

    Hi,

    I'm starting my first umbraco project and have my first MVC views working fine. I have a simple controller action in which I would like to retrieve the children of the current item. My action looks like this:

    var posts = CurrentPage.Children.Where(x=> ????)
    

    I have been searching for quite a while now but can't seem to find anything explaining how to only load children that were published. There doesn't seem to be a property or method such as IsPublished. I have tried several different ways of filtering the children like

    var posts = CurrentPage.Children.Where(x=> !x.IsDraft)
    

    But I always get all children (I have two published test items and 2 not published items), whether they're published or not. Any ideas or suggestions on how to solve this?

    Thanks Ben

  • ozb3n 4 posts 34 karma points
    Jul 20, 2015 @ 06:10
    ozb3n
    0

    In case anyone is looking for an answer to this, I think I worked it out:

    var contentService = new ContentService();
    var posts = contentService.GetChildren(CurrentPage.Id).Where(x => x.Status == ContentStatus.Published);
    

    This seems to give me only the published items.

  • Stephen 767 posts 2273 karma points c-trib
    Jul 20, 2015 @ 09:01
    Stephen
    101

    CurrentPage.Children should only return published content, but default, built-in. Non-published content are not returned to views (unless previewing). So... you should not need a filter at all.

    Does that make sense?

  • ozb3n 4 posts 34 karma points
    Jul 20, 2015 @ 09:50
    ozb3n
    0

    Hi Stephen,

    Thanks for pointing me into the right direction it was preview mode which was causing all items to be returned! With preview mode off, it all works as expected :)

    Thank you!

Please Sign in or register to post replies

Write your reply to:

Draft