Copied to clipboard

Flag this post as spam?

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

  • David Gregory 76 posts 202 karma points
    Jul 28, 2017 @ 11:42
    David Gregory

    How to access nodes when there is more than 1 root

    Hello all

    I'm having trouble getting the code right to list the nodes as shown in the image.

    I did have the Events Organiser as a child in Home but had to move it out when our site went bilingual. Previous code that worked when it was contained in Home is below.

    var selection = Model.Content.Site().FirstChild("eventsAdmin").FirstChild("eventHolder").Children("event").where .....

    But how should I tweak that to get the same result?

    enter image description here

  • Nik 1512 posts 6673 karma points MVP 4x c-trib
    Jul 28, 2017 @ 12:26

    Hi David,

    You have a few options, however the easiest in my opinion would be add a property on to your home page document type where you select the events container node.

    Then you can do the following:

    var site = Model.Content.Site();
         var eventsContainer = Umbraco.TypedContent(site.GetPropertyValue("eventContainerNode"));
        if(eventsContainer != null)
              var events = eventsContainer.Children("event");

    This also gives you scope for having language specific events if you need it.

    Another option is to utilise Examine to get the events, that way you can filter them by up coming ones, assuming they have a date on them, as well and exclude past ones if needed.


  • Sven Geusens 169 posts 878 karma points c-trib
    Jul 28, 2017 @ 13:38
    Sven Geusens

    Hey david

    Model.Content.Site or Model.Content.AncestorOrSelf(1) goes from the node you are on to the node at level 1 (the root) so you cannot go to another rootnode

    Umbraco.TypedContentAtRoot() gives you all root nodes in the umbraco.

    so to get the (first) events organiser from anywhere you can do

    Umbraco.TypedContentAtRoot().First(c => c.DocumentTypeAlias == "EventsOrginiserDocumentTypeAlias")

    So to complete your example

    Umbraco.TypedContentAtRoot().First(c => c.DocumentTypeAlias=="eventsAdmin").FirstChild("eventHolder").Children("event").where...

    As a remark: If you go this approach, having 1 section (repository) for All your events over multiple languages, then it might be a good idea to wrap the properties on the events in Vorto properties. This way you can make them multilingual as well.

  • David Gregory 76 posts 202 karma points
    Jul 28, 2017 @ 13:56
    David Gregory

    Thanks guys some good suggestions there.

Please Sign in or register to post replies

Write your reply to: