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

发布于 2024-10-17 17:04:00 字数 94 浏览 5 评论 0原文

使用 Umbraco 4.6+,有没有办法在 C# 中检索特定文档类型的所有节点?我一直在 umbraco.NodeFactory 命名空间中查找,但尚未找到任何有用的东西。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

故人的歌 2024-10-24 17:04:00

我今天刚刚这样做,类似下面的代码应该可以工作(使用 umbraco.presentation.nodeFactory),使用 -1 的 nodeId 调用它来获取站点的根节点并让它向下工作:

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

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);
    }
}
最丧也最甜 2024-10-24 17:04:00

假设您最终只需要几个特定类型的节点,那么使用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;
}

因此,按类型获取节点的最终调用将是:

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

因此,如果您只想要获得前 5 个结果,您可以将 .Take(5) 添加到末尾,这样您将仅递归前 5 个结果,而不是提取整个树。

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.

栖竹 2024-10-24 17:04:00

或者递归方法:

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

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...
}
像你 2024-10-24 17:04:00

如果您只是创建一个供宏使用的 razor 脚本文件(Umbraco 4.7+),我发现这个速记特别有用......

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

希望这对某人有帮助!

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!

烟织青萝梦 2024-10-24 17:04:00

在 umbraco 7.0+ 中你可以这样做

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

In umbraco 7.0+ you can do it like this

foreach (var childNode in node.Children<ChildNodeType>())
{
...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文