Copied to clipboard

Flag this post as spam?

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


  • Manish 373 posts 932 karma points
    May 23, 2016 @ 08:56
    Manish
    1

    Get parent of parent .. parent of current node

    How can i get parent .. parent till four node of current node in razor ?

    I am using like this

    var pageId = umbraco.uQuery.GetCurrentNode().Id; var node = new Node(pageId);

    node = node.Parent.Parent.Parent.Parent;

    But is there any better way to use parent by it's type

    Thanks

  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    May 23, 2016 @ 09:14
    Kevin Jump
    2

    Hi

    it depends a bit on what you do, if you get the typed version of a node using Model.Content you can get the level and then go four higher?

    var parentFour = Model.Content.Ancestor(Model.Content.Level - 4);
    

    but this has risks (if you are less than 4 deep - you will be asking for a negative level.

    if you parent 4 up has it's own doctype you can ask for the first ancestor that has that

    var parentType = Model.Content.Ancestor("Parent4Type");
    
  • Manish 373 posts 932 karma points
    May 26, 2016 @ 05:47
    Manish
    0

    Thanks for replying on this

    I have done like this finally

      var nodes = uQuery.GetCurrentNode().GetAncestorOrSelfNodes().Where(x => x.NodeTypeAlias == "alias");
    

    Manish

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 19, 2016 @ 18:12
    Alex Skrypnyk
    2

    Just remember that uQuery is obsolete.

    Read more here: https://our.umbraco.org/forum/developers/api-questions/45777-uQuery-vs-Examine-vs-IPublishedContent-for-Querying

    Thanks,

    Oleksandr Skrypnyk

Please Sign in or register to post replies

Write your reply to:

Draft