Copied to clipboard

Flag this post as spam?

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


  • Jack Fisher 4 posts 84 karma points
    Apr 02, 2019 @ 16:03
    Jack Fisher
    0

    Selecting a node from master template

    I'm new to using Umbraco (I started a few days before V8 was launched) and I am struggling to work out the Razor syntax for selecting a specific node in my page structure when I'm on the master template.

    My page structure looks like this:

    enter image description here

    I would like to select the Case Studies page from the master template so that I can list out the children of the case studies page.

    This is what I have done on my master template (it works but I don't like it):

            var navItems = Model.Root().Children().Where(x => x.IsVisible());
            var homePage = Model.Root();
    
    
                            var caseStudiesNode = Model.Root();
    
                            foreach (var item in navItems)
                            {
                                if (item.Id == 1093)
                                {
                                    caseStudiesNode = item;
                                }
                            }
    

    This feels like a hacky way to do it and I'm sure theres a better way, can anyone help?

  • Dennis Flæng Jørgensen 35 posts 145 karma points c-trib
    Apr 02, 2019 @ 16:49
    Dennis Flæng Jørgensen
    1

    Hey Jack

    If it was me i would create a specific document type for my case studies and find the first descendant of my root page of that document type.

    Alternatively you could create a property on the document type you have made for your frontpage with a Content Picker. Then in razor use the GetPropertyValue

  • Jack Fisher 4 posts 84 karma points
    Apr 02, 2019 @ 20:39
    Jack Fisher
    0

    Thanks for the advice, I tried accessing the first descendant of that document type but it wouldn't let me run .Children() on that variable that I stored it in.

  • Thomas Kassos 54 posts 265 karma points
    Apr 02, 2019 @ 17:08
    Thomas Kassos
    100

    you could do it with the new ugly way

    var selection = Umbraco.Content(Guid.Parse("1d770f10-d1ca-4a26-9d68-071e2c9f7ac1"));
    

    or as Dennis said, create a new doc type for case studies and do something like those examples

    @Model.Root().FirstChildOfType("caseStudies");
    
    @Model.Root().Children().Where( x =>x.ContentType.Alias == "caseStudies").FirstOrDefault();
    
    @Model.Root().DescendantOfType("caseStudies");
    

    Not sure what will work best in this version

    Cheers

  • Jack Fisher 4 posts 84 karma points
    Apr 03, 2019 @ 09:36
    Jack Fisher
    0

    I got it to work with this which is better than my foreach loop:

    var selection = Umbraco.Content(Guid.Parse("1d770f10-d1ca-4a26-9d68-071e2c9f7ac1"));
    

    Thanks for your help!

  • Bryna 73 posts 259 karma points
    Apr 03, 2019 @ 15:05
    Bryna
    1

    To grab by a specific ID seems to be asking for trouble. The code below might be more akin to what you are looking for, BUT you will have to surround it with your own checks(eg What happens when you remove that node completely? I have had this happen before:) ) Below is yet another alternative:

    Model.FirstChild<CaseStudiesList>().Children<CaseStudies>()
                            .Where(x => x.IsVisible())
    
  • Jack Fisher 4 posts 84 karma points
    Apr 03, 2019 @ 15:35
    Jack Fisher
    0

    This is a great alternative which I have decided to implement, thanks for the help!

Please Sign in or register to post replies

Write your reply to:

Draft