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

Jeremy Wiggins picture Jeremy Wiggins · Feb 16, 2011 · Viewed 15.1k times · Source

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.

Answer

sebastiaan picture sebastiaan · Feb 16, 2011

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