Copied to clipboard

Flag this post as spam?

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


  • Paul Griffiths 370 posts 1021 karma points
    Nov 03, 2015 @ 13:11
    Paul Griffiths
    0

    Getting node by Id in Razor

    Hi All,

    I’m after some advice on whether my approach is the right one for accessing specific nodes and their children. I have the following structure

    Content (is this level 0 or level -1?)

    Home (Level 1)

    About (Level 2)

    Interests (Level 2)

    Hobbies (Level 2)

    Blog (Level 2)

    Blog Post 1 (Level 3)

    Blog Post 2 (Level 3)

    Carousel Repository (Level 1)

    Carousel Slide 1 (Level 2)

    Carousel Slide 2 (Level 2)

    In order to access the Blog children (blog post 1 & 2) I have been using the following code

        @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
        @{ var blogNode = Umbraco.Content(1000);  } (id of blog node) 
    
        @foreach (var blogPost in blogNode.Children)
          { 
              //access all of the properties of each blog post and render
    }
    

    Is this the correct approach to use? In addition, I have a couple more questions im hoping someone can help with while im trying to learn razor with umbraco.

    1. is the content node level 0 or -1?
    2. What does .Site() do e.g. currentPage.Site()

    Im struggling to work out which appraoch to use with regards to @Umbraco, @CurrentPage or @Model in Razor views

    Hopefully someone can point me in the right direction

    Many thanks

    Paul

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 03, 2015 @ 17:01
    Dennis Aaen
    101

    Hi Paul,

    I would recommend you to not use id´s if possible when you query content. The reason for this is that the id will change if the user delete the node and re-created it again.

    You can use the Razor code snippet below, which should do what you want.

    @{
        var selection = CurrentPage.AncestorOrSelf(1).Descendants("Blog").Where("Visible").FirstOrDefault();
    }
    <ul>
        @foreach(var item in selection.Children.Where("Visible")){
            <li>
                <a href="@item.Url">@item.Name</a>
            </li>
        }
    </ul>
    

    Remember to change the Blog in the descendants statement, so it match your document type alias for your blog.

    The content node has the level of 1. The CurrentPage.Site, top node of the site. e.g In your structure it will be the Home node. @CurrentPage is used for dynamic Razor. The Model is used for strongly typed Razor if you have your own model. You can also see Model.Content, this is strongly type too.

    The @Umbraco is used for the Umbraco helpers, it could be that you need a dictionary item, or strip the HTML of a string or truncate some string. You can find the documentation for the Umbraco helpers here: https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/

    For the dynamic razor and strongly typed, try to see this documentation. https://our.umbraco.org/documentation/Reference/Templating/Mvc/partial-views and https://our.umbraco.org/documentation/Reference/Templating/Macros/Partial-View-Macros/

    Hope this helps,

    /Dennis

  • Paul Griffiths 370 posts 1021 karma points
    Nov 04, 2015 @ 08:53
    Paul Griffiths
    0

    Hi Dennis,

    Thanks for taking out the time to provide all that information. It really helped to clear some things up :). It makes perfect sense not to use id's incase these nodes are deleted by the editors. I knew I wasnt quite on the right lines.

    The code that you provided seems to work perfectly for me, thank you. Just so im fully understanding it can i just verify that my thinking is correct please

    From the current page traverse up the tree as far as level 1 (typically the home node)

    CurrentPage.AncestorOrSelf(1)
    

    When we reach the 'home' node find all the descendants which have a document type alias of 'Blog'

    .Descendants("Blog")
    

    only display nodes where 'umbracoNaviHide' (true/false property added to the docType) has not been checked

    .Where("Visible")
    

    Pull the first item out the list (not sure what default does)

    .FirstOrDefault()
    

    Display the property values from the list item returned

    <ul>
    @foreach(var item in selection.Children.Where("Visible")){
        <li>
            <a href="@item.Url">@item.Name</a>
        </li>
    }
    

    One final question, can you point me in the direction where i can find some information on how to populate a list to a specific amount of items. For example, if i have 10 blog posts below the blog node but i only want to return the latest 3 and display them. I think in XSLT you used to specify a maxItems var.

    Hope that makes sense and thanks for your help

    Paul

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 04, 2015 @ 09:16
    Dennis Aaen
    1

    Hi Paul,

    You are right in your step by step reading of the code. The FirstOrDefualt() make sure that you just get the first blog node, if you have more that one.

    You question about only show the latest 3 items. The you can use the Take() method. So your code should look like this.

    <ul>
    @foreach(var item in selection.Children.Where("Visible").Take(3).OrderBy("CreateDate, desc")){
        <li>
            <a href="@item.Url">@item.Name</a>
        </li>
    }
    </ul>
    

    To get a better understanding which methods you have in Razor, you could take a look at the Razor cheat sheets. I know it´s says for Umbraco 6, but they will also work for Umbraco 7. https://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets

    It´s some pdf files with an overview.

    Hope this helps,

    /Dennis

  • Paul Griffiths 370 posts 1021 karma points
    Nov 04, 2015 @ 09:49
    Paul Griffiths
    0

    Hi Dennis,

    Awesome!! thanks for your feedback. At least im slowly getting there.

    Im so glad that there is a method to filter out the latest three items that will make things a whole lot easier.

    Once again thanks for your help with answering my questions.

    Many thanks Paul

Please Sign in or register to post replies

Write your reply to:

Draft