jsg
jsg

Reputation: 1264

Umbraco: Get root Node ID based on Node ID

I'm new to Umbraco, the issue that I have a 5 root nodes, and I've got a list of random pages that are contained within those root nodes. The data I'm receiving back from these pages are NodeId, NodeName and Level. What I'm trying to do is get root node information for each of the pages I have. Unfortunately this is where I'm getting issues, is there a way to get the root node or level 1 node's information based on a NodeId.

This is what I've got so far:

foreach (var item in pages)
{
    int level = item["level"].AsInt();
    if (level > 1){
        var currentItem = library.GetCurrentDomains(item.Id);
    }
}

Ive tried library.GetCurrentDomains(item.Id) however this doesnt work.

Upvotes: 1

Views: 1108

Answers (2)

Tim
Tim

Reputation: 4257

Assuming the list of random pages are all IPublishedContent, you can use the extension method AncestorOrSelf(1) on the page, which will get the root node for the page. E.g.

foreach (var item in pages)
{
    var rootPage = item.AncestorOrSelf(1);

    //do something with the root node here
}

Upvotes: 1

Jannik Anker
Jannik Anker

Reputation: 3425

Not entirely sure if this is what you need, nor if it's the best way, but you could do something like

item.Path.Split(',')[1]

to get the second level "root" of any node. I think ;-)

Upvotes: 1

Related Questions