1
votes

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.

2
Please, share more code and context around it. How you're retrieving this data first (IDs) and why you're not using any Umbraco wrappers to get dynamic or typed objects of documents and then take ability of traversing XML cache by using Parent / AncestorOrSelf etc. helper methods?Marcin Zajkowski

2 Answers

1
votes

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 ;-)

1
votes

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
}