Copied to clipboard

Flag this post as spam?

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


  • Martin 7 posts 27 karma points
    Sep 27, 2015 @ 17:01
    Martin
    0

    Find document type in complex structure Umbraco 7

    My content is structured in the following way.

    Root
        Website A
            Home
                Page 1
                Page 2
                Page N
        Website B
            Home
        Website C
            Home
    

    Each Home document type is connected to an master layout template. In my master layout I try to find the Home node by writing this code.

    var home = Model.Content.Siblings().FirstOrDefault(x => x.DocumentTypeAlias == "Home");

    This works when im on the Home node, but when I go to ex. Page 1 I get an exception, because Home is not a sibling for Page 1.

    Then I tried this.

    var home= Umbraco.TypedContentAtXPath("//Home").FirstOrDefault();

    And this worked when I was at Website A since it returned the first node. But when at Website B I still got Website A node, and that is wrong.

    Any tips on how to solve this?

    Originally posted at: http://stackoverflow.com/questions/32809839/find-document-type-in-complex-structure-umbraco-7

  • Tom Van Rompaey 38 posts 136 karma points
    Sep 27, 2015 @ 17:35
    Tom Van Rompaey
    0

    I think this is the code that you are looking for:

    var home = Model.Content.AncestorOrSelf("Home");

  • Martin 7 posts 27 karma points
    Sep 27, 2015 @ 18:18
    Martin
    0

    Thanks! I had to change it a bit for it to work.

    var home = Model.Content.AncestorOrSelf(2).Children().FirstOrDefault(x => x.DocumentTypeAlias == "Home");
    
Please Sign in or register to post replies

Write your reply to:

Draft