Copied to clipboard

Flag this post as spam?

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


  • EsqJ 11 posts 54 karma points
    Feb 16, 2015 @ 17:19
    EsqJ
    0

    get the page id of the parent?

    Hi guys,

    This sounds simple but i can't work it out, i just want to write to screen the page id of the parent of the current page. lets say i have this structure..

    Home
      About (page id 100)
         ChildPage

    If im on ChildPage, how do i get the ID of the about page? i know to get the current page id i can use 'Umbraco.Field("pageID")' but trying Umbraco.Field("ParentId") does not write out the parent id (it actually writes out nothing)

     

     

     

     

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 16, 2015 @ 17:40
    Dennis Aaen
    0

    Hi EsqJ,

    The Umbraco.Field refers to the current page, if you see docuementation for Umbraco.Field here https://our.umbraco.org/documentation/Reference/Mvc/views#RenderingafieldwithUmbracoHelper - it says, This is probably the most used method which simply renders the contents of a field for the current content item.

    To get the parent page id, you can do it in razor like this:

    @CurrentPage.Parent.Id

    Hope this helps,

    /Dennis

  • EsqJ 11 posts 54 karma points
    Feb 16, 2015 @ 17:49
    EsqJ
    0

    Excellent, just what i needed, Thanks!

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 16, 2015 @ 17:54
    Dennis Aaen
    0

    Hi EsqJ,

    Super - glad that I could help you. I think that you should mark the question as solve so other that comes accross this post can go directly to the solution. You do this by click on the green tick on the left side close to the avatar image of the item which gave you the solution to your question.

    Once a again glad that I could help you, and happy Umbraco coding.

    /Dennis

  • EsqJ 11 posts 54 karma points
    Feb 16, 2015 @ 17:59
    EsqJ
    0

    Just a quick question, the way im using this means this code also ends up on the home page via a partial view, as the home page has no parent i get an error '

    Cannot perform runtime binding on a null reference'

    is there a way around this?

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 16, 2015 @ 18:05
    Dennis Aaen
    0

    Hi EsqJ,

    Try this:

    @{
        if(CurrentPage.Parent != null){
            @CurrentPage.Parent.Id
        }
    }

    Hope this helps,

    /Dennis

  • EsqJ 11 posts 54 karma points
    Feb 18, 2015 @ 17:28
    EsqJ
    0

    Hi Dennis,

    That worked but i have come across an issue of my own making.

    If i have this structure...

    Home
      About (page id 100)
         ChildPage
            ChildOfChild

    The parent of childofchild is going to be child page, but i still want to get the id of the *root* parent (about us). Does that make sense?

     

     

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 18, 2015 @ 18:01
    Dennis Aaen
    0

    Hi EsqJ,

    Then you could do something like this:

    @Umbraco.Content(100).Id

    You can read about Umbraco.Content here: https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/#Content%28intid%29 if you are using strongly typed razor the snippet looks like this:

    @Umbraco.TypedContent(100).Id

    You could also create a variable like this:

    @(
        var = aboutPageId = Umbraco.Content(100);
        @aboutPageId.Id
    }

    Hope this helps,

    /Dennis

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 18, 2015 @ 18:12
    Dennis Aaen
    0

    Hi EsqJ,

    And if your about page has it´s own document type then you can do it like this also.

    @CurrentPage.AncestorOrSelf(1).Descendants("Document Type Alias").FirstOrDefault().Id

    Remember to change the Document Type Alias so it match your about page document type alias.

    Hope this helps,

    /Dennis

Please Sign in or register to post replies

Write your reply to:

Draft