简体   繁体   English

Umbraco 4.6+ - 如何通过C#中的doctype获取所有节点?

[英]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#? 使用Umbraco 4.6+,有没有办法在C#中检索特定doctype的所有节点? I've been looking in the umbraco.NodeFactory namespace, but haven't found anything of use yet. 我一直在寻找umbraco.NodeFactory命名空间,但还没有发现任何有用的东西。

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: 我今天刚刚这样做,类似下面的代码应该工作(使用umbraco.presentation.nodeFactory),用nodeId为-1调用它来获取网站的根节点并让它工作下来:

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

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: 假设您最终只需要特定类型的几个节点,那么使用yield关键字以避免检索超过以下内容会更有效:

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. 因此,如果你只想获得前五个,你可以添加.Take(5)到最后,你只会通过前5个结果递归,而不是拉出整个树。

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...
}

If you're just creating a razor scripting file to be used by a macro (Umbraco 4.7+), I found this shorthand particularly useful... 如果您只是创建一个由宏(Umbraco 4.7+)使用的剃刀脚本文件,我发现这个简写特别有用......

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

Hope this helps somebody! 希望这有助于某人!

In umbraco 7.0+ you can do it like this 在umbraco 7.0+中你可以这样做

foreach (var childNode in node.Children<ChildNodeType>())
{
...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM