Copied to clipboard

Flag this post as spam?

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


  • Dmitriy 168 posts 588 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 6182 posts 24284 karma points MVP 8x 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 168 posts 588 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 6182 posts 24284 karma points MVP 8x admin c-trib
    Nov 01, 2018 @ 12:03
    Alex Skrypnyk
    1

    It's fine in performance

  • Garðar Þorsteinsson 119 posts 566 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 1286 posts 4060 karma points MVP 8x c-trib
    Jun 19, 2019 @ 13:00
    Bjarne Fyrstenborg
    2

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

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

    /Bjarne

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies