Umbraco 4.6+ - How to get all nodes by doctype in C#?
Asked Answered
G

5

17

Using Umbraco 4.6+, is there a way to retrieve all nodes of a specific doctype in C#? I've been looking in the umbraco.NodeFactory namespace, but haven't found anything of use yet.

Gonick answered 16/2, 2011 at 16:33 Comment(0)
D
16

I was just doing this today, something like the below code should work (using umbraco.presentation.nodeFactory), call it with a nodeId of -1 to get the root node of the site and let it work it's way down:

private void DoSomethingWithAllNodesByType(int NodeId, string typeName)
{
    var node = new Node(nodeId);
    foreach (Node childNode in node.Children)
    {
        var child = childNode;
        if (child.NodeTypeAlias == typeName)
        {
            //Do something
        }

        if (child.Children.Count > 0)
            GetAllNodesByType(child, typeName);
    }
}
Defendant answered 16/2, 2011 at 18:26 Comment(1)
Using -1 as the id to get the root node of the site is a great tip! Thanks for thatSoot
W
15

Supposing you only eventually need a couple of nodes of the particular type, it would be more efficient to use the yield keyword to avoid retrieving more than you have to:

public static IEnumerable<INode> GetDescendants(this INode node)
{
    foreach (INode child in node.ChildrenAsList)
    {
        yield return child;

        foreach (INode grandChild in child.GetDescendants())
        {
            yield return grandChild;
        }
    }
    yield break;
}

So your final call to get nodes by type will be:

new Node(-1).GetDescendants().Where(x => x.NodeTypeAlias == "myNodeType")

So if you only want to get the first five, you can add .Take(5) to the end and you will only recurse through the first 5 results rather than pull out the whole tree.

Wahoo answered 2/12, 2011 at 17:39 Comment(0)
I
3

Or a recursive approach:

using umbraco.NodeFactory;

private static List<Node> FindChildren(Node currentNode, Func<Node, bool> predicate)
{
    List<Node> result = new List<Node>();

    var nodes = currentNode
        .Children
        .OfType<Node>()
        .Where(predicate);
    if (nodes.Count() != 0)
        result.AddRange(nodes);

    foreach (var child in currentNode.Children.OfType<Node>())
    {
        nodes = FindChildren(child, predicate);
        if (nodes.Count() != 0)
            result.AddRange(nodes);
    }
    return result;
}

void Example()
{
    var nodes = FindChildren(new Node(-1), t => t.NodeTypeAlias.Equals("myDocType"));
    // Do something...
}
Irreproachable answered 12/8, 2011 at 10:8 Comment(0)
P
1

If you're just creating a razor scripting file to be used by a macro (Umbraco 4.7+), I found this shorthand particularly useful...

var nodes = new Node(-1).Descendants("DocType").Where("Visible");

Hope this helps somebody!

Pansy answered 24/1, 2013 at 17:20 Comment(0)
W
1

In umbraco 7.0+ you can do it like this

foreach (var childNode in node.Children<ChildNodeType>())
{
...
}
Walli answered 26/8, 2016 at 11:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.