Umbraco 4.6+ - 如何在C#中通过doctype获取所有节点?
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我今天刚刚这样做,类似下面的代码应该可以工作(使用 umbraco.presentation.nodeFactory),使用 -1 的 nodeId 调用它来获取站点的根节点并让它向下工作:
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:
假设您最终只需要几个特定类型的节点,那么使用yield关键字会更有效,以避免检索超出您需要的内容:
因此,按类型获取节点的最终调用将是:
因此,如果您只想要获得前 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:
So your final call to get nodes by type will be:
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.
或者递归方法:
Or a recursive approach:
如果您只是创建一个供宏使用的 razor 脚本文件(Umbraco 4.7+),我发现这个速记特别有用......
希望这对某人有帮助!
If you're just creating a razor scripting file to be used by a macro (Umbraco 4.7+), I found this shorthand particularly useful...
Hope this helps somebody!
在 umbraco 7.0+ 中你可以这样做
In umbraco 7.0+ you can do it like this