Copied to clipboard

Flag this post as spam?

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


  • Fengelz 106 posts 221 karma points
    Apr 29, 2011 @ 13:51
    Fengelz
    0

    Getting Rootnode siblings

    I need to fetch the current node's rootnode siblings. I figured I could do it this way: 

    @{ 
        var rootNode = Model.AncestorOrSelf(1);
        var rootSiblings = Model.AncestorsOrSelf(1).Where("Id!="+rootNode.Id);
    }
    <p>@rootSiblings.First().Level</p> <--renders "2"
    <ul class="lang-ul">
    @foreach (var node in rootSiblings)
    {
        <li style="background:url(@Model.MediaById(node.flag).UmbracoFile) no-repeat">
            <a  href="@node.Url">@node.Name</a>
        </li>
    }
    </ul>

    Bottom line: the rootSiblings first node is of level 2, which I assume means that no nodes match "Id!="+rootNode.Id.

    The rootNode.Id is correct, but I cant figure out what's semantically wrong. Then again I can't figure out if the Linq extension Where clause is correct.

    Any help is appreciated, Perhaps if anyone has a different approach?

    best regards 

    - Sune

  • Sebastiaan Janssen 4890 posts 14567 karma points MVP admin hq
    Apr 30, 2011 @ 13:35
    Sebastiaan Janssen
    0

    Try this (untested):

    @{ var rootNode = Model.AncestorsOrSelf(1); }
    <p>@Model.AncestorOrSelf().Children.Where("Id!="+rootNode.Id).First().Level</p>
  • Fengelz 106 posts 221 karma points
    May 02, 2011 @ 14:03
    Fengelz
    0

    I ended up using xpath like this: 

    var rootNodes = Model.XPath("//ancestor-or-self::*[@level=1 and name() = 'Site' and @id != " + Model.AncestorOrSelf().Id + "]");

    The problem with my first approach, it seems, is that its impossible to get the sibling nodes of the root node as they are not ancestors of the current node. It seems logical enough, but luckily it works with xPath.

     

    best regards

    - Sune

  • Thomas Enggaard Christensen 23 posts 48 karma points
    May 02, 2011 @ 14:36
    Thomas Enggaard Christensen
    0

    I have not tried any @Razor scripting in Umbraco yet, so this is purely speculation and guessing on what methods are available on the @Model object.

    But wont something like this do the trick:

    @{ 
       
    var rootNode = Model.AncestorOrSelf(1);
       
    var rootSiblings = rootNode.Parent.Children.Where("Id!="+rootNode.Id);
    }

     

  • Fengelz 106 posts 221 karma points
    May 02, 2011 @ 14:43
    Fengelz
    0

    Logically yes.

    But the rootnode in this case is a sitenode at level 1, which means that its parent is the content node.

    I just tested your snippet which throws a null refference exception "Cannot perform runtime binding on a null reference".

    I believe that this happens when you try to fetch the parent of a conent node at level 1.

  • Thomas Enggaard Christensen 23 posts 48 karma points
    May 02, 2011 @ 18:16
    Thomas Enggaard Christensen
    3

    Weird...

    Anyways :) I took this as an excuse to take a look at the Razor scripting in umbraco. Looks pretty nice.

    I did a small PoC using this as my goal.

    Tested and working as intended :)

    var contentRoot = new Node(-1);
    var
    children = contentRoot.ChildrenAsList.Where(n => n.Id != Current.Id);

     

  • Fengelz 106 posts 221 karma points
    May 02, 2011 @ 18:25
    Fengelz
    0

    Didn't think of that approach.

    I'll keep it in mind til next time :).

     

  • Barry Fogarty 493 posts 1129 karma points
    Oct 06, 2011 @ 05:41
    Barry Fogarty
    1

    Also to chip in here, this works nicely using DynamicNode (in v4.7 +)

    @using umbraco.MacroEngines;

    @{
    DynamicNode contentRoot = new DynamicNode(-1);
    foreach (DynamicNode n in contentRoot.Descendants("Doc Type Alias"))

     

    (in my case I needed to iterate through a sibling folder to the website tree)

  • Garðar Þorsteinsson 96 posts 469 karma points
    Nov 20, 2013 @ 21:27
    Gar├░ar ├×orsteinsson
    3

    This is an old post but it got me in the right direction, here is another way for version 6

    @{
        var model = Umbraco.TypedContentAtRoot();
    }
    
    @foreach (var item in model.Where(x => x.IsVisible())) { 
        @item.Name
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft