Copied to clipboard

Flag this post as spam?

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


  • Dmitriy 135 posts 510 karma points
    Nov 01, 2018 @ 10:12
    Dmitriy
    0

    Get current root node from nested content partial view

    Hello, Umbracians.

    Sometimes website have a lot of root nodes, in multilingual case for example.

    In common, there is no problem to get a current root node with @Model.Ancestor<HomePage>(), but there is when you do it in a partial view of nested content

    I'am confused. How to get a current root node from partial view of nested content?

  • Alex Skrypnyk 5329 posts 19613 karma points MVP 3x admin c-trib
    Nov 01, 2018 @ 11:52
    Alex Skrypnyk
    100

    Hello Dmitriy

    You can get the current page Id with this code:

    @UmbracoContext.Current.PageId
    

    And then get current root node with code:

    var currentPage = Umbraco.TypedContent(UmbracoContext.Current.PageId);
    var root = currentPage.Ancestor<HomePage>();
    

    Thanks,

    Alex

  • Dmitriy 135 posts 510 karma points
    Nov 01, 2018 @ 11:56
    Dmitriy
    0

    Hi, Alex

    Thank you, seems it what I need.

    Is it optimal performance way, or has some leaks?

  • Alex Skrypnyk 5329 posts 19613 karma points MVP 3x admin c-trib
    Nov 01, 2018 @ 12:03
    Alex Skrypnyk
    1

    It's fine in performance

  • Garðar Þorsteinsson 92 posts 463 karma points
    Nov 01, 2018 @ 12:19
    Gar├░ar ├×orsteinsson
    1

    Hi,

    Would it not be better to use:

        var currentNode = UmbracoContext.Current.PublishedContentRequest.PublishedContent;
        var root = currentNode.Site();
    
  • Bjarne Fyrstenborg 1140 posts 3225 karma points MVP 3x c-trib
    Jun 19, 2019 @ 13:00
    Bjarne Fyrstenborg
    1

    Alternative access IPublishedContent via UmbracoHelper - Umbraco property in UmbracoViewPage class.

    var currentNode = Umbraco.AssignedContentItem.Root();
    var site = currentNode.Root();
    

    /Bjarne

Please Sign in or register to post replies

Write your reply to:

Draft