Copied to clipboard

Flag this post as spam?

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


  • Lukaz 44 posts 202 karma points
    Mar 03, 2020 @ 14:19
    Lukaz
    0

    Umbraco 8 - How to get all page nodes using alias?

    Right now I've site structure like this in backoffice:

    this is just illustration of node structure.

    Home |-Events |-Category1 |-Event post 1 |-Event post 2 ... |-Category2 |- Event post 3 |-Event post 4 ... and so on

    enter image description here

    All event post have "eventPost" alias

    Now I want to show four event posts ordered by created date desc on my home page. How to do that? I tried to built query but don't know how to get all event posts based on alias and Events as parent node.

    Any help would be great.

    Something like this:

    eventsGuid = "0680768a-2f39-48aa-a83b-a465e75e9bf1";
    var eventPosts = Umbraco.Content(Guid.Parse(eventsGuid))
        .Children()
        .Where(x => x.IsVisible() &&  x.IsDocumentType("eventPost"))
        .OrderByDescending(x => x.CreateDate)
    .Take(4);
    
  • Marc Goodson 2138 posts 14321 karma points MVP 8x c-trib
    Mar 03, 2020 @ 18:46
    Marc Goodson
    0

    Hi Lukaz

    Would the following be what you are after:

        var eventPosts = Umbraco.Content(Guid.Parse(eventsGuid))
    .Descendants()
    .Where(x => x.IsVisible() && x.IsDocumentType("eventPost"))
    .OrderByDescending(x => x.CreateDate);
    

    Essentially 'Children()' will only be looking 'one depth' immediately below your Events node - whereas Descendants, will look at all Children and Children of Children etc

    regards

    Marc

  • Lukaz 44 posts 202 karma points
    Mar 04, 2020 @ 07:45
    Lukaz
    100

    Hi Marc, thank you for your effort. The given code doesn't resolve my problem, but I've managed to get it work and down below is my solution.

    var eventPosts = Umbraco.ContentAtRoot().FirstOrDefault()
        .Descendants()
        .Where(x => x.IsVisible() && x.IsDocumentType("eventPost"))
        .OrderByDescending(x => x.CreateDate)
        .Take(4);
    
Please Sign in or register to post replies

Write your reply to:

Draft