Search code examples
c#apiumbraco

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


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.


Solution

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