What version of umbraco you using the latest version you can make use of linq2umbraco and you can get nodes back recursively with <4.1 you need to write recursive function. If you are in .net macro you could also use umbraco.content.Instance which returns xml document and you then xpath into that //node[@nodeTypeAlias='Event Item'] however that could become slow if you have alot of content.
So, encapsulate in static class and the method "magically" gets tagged onto the node. Gotta love C#!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco.presentation;
using umbraco.presentation.nodeFactory;
public static class ExtensionMethods
{
public static IEnumerable<Node> AllDescendants(this Node node)
{
foreach (Node child in node.Children)
{
yield return child;
foreach (Node grandChild in child.AllDescendants())
yield return grandChild;
}
}
}
Then you can do something like this...
umbraco.presentation.nodeFactory.Node node = new umbraco.presentation.nodeFactory.Node(1234);
var all = node.AllDescendants();
Hope this helps someone else, if not the original poster!
Quickest way in .NET to get all nodes by nodeTypeAlias?
Hi All,
I'm writing something for a client which needs to find all the nodes which have the nodeTypeAlias = 'Event Item'.
These can sit on many levels through out the site structure.
So the node.Children() method wont be right as it only seems to return the one level.
So the question is, how do I get all the nodes with nodeTypeAlias = 'Event Item' and whats the quickest way?
Thanks
Tom
Tom,
What version of umbraco you using the latest version you can make use of linq2umbraco and you can get nodes back recursively with <4.1 you need to write recursive function. If you are in .net macro you could also use umbraco.content.Instance which returns xml document and you then xpath into that //node[@nodeTypeAlias='Event Item'] however that could become slow if you have alot of content.
regarsd
Ismail
You can use this snippet, copied from someone else but very usefull:
I dont know whether its just late on in the day or i've been looking at code for too long but am I being thick here.
I've tried making a call to that snippet but not sure what to pass into it or read the collection.
Thanks
Tom
Samle is in the comments:
It returns a collection of nodes (IEnumerable<Node>).
You can setup somethinglike
Or bind de nodescollection to a databound control:
Hi,
Just needed to use this myself, without much experience in extension methods so referenced the following doc
http://msdn.microsoft.com/en-us/library/bb383977.aspx
So, encapsulate in static class and the method "magically" gets tagged onto the node. Gotta love C#!
Then you can do something like this...
Hope this helps someone else, if not the original poster!
Cheers,
Tom
Thanks Tom,
Spent 2 hours this morning looking for a solution for this as the Razor script wasn't going to do what I wanted.
Thanks for sharing!
Adam
is working on a reply...