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?
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.
Getting Rootnode siblings
I need to fetch the current node's rootnode siblings. I figured I could do it this way:
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
Try this (untested):
I ended up using xpath like this:
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
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:
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.
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 :)
Didn't think of that approach.
I'll keep it in mind til next time :).
Also to chip in here, this works nicely using DynamicNode (in v4.7 +)
(in my case I needed to iterate through a sibling folder to the website tree)
This is an old post but it got me in the right direction, here is another way for version 6
is working on a reply...