I'm looking at the Parent property of umbraco.presentation.nodeFactory.Node, and am thinking of creating some kind of recursive method call to do it. But I was wondering if there was some method of doing this in the API already. So I didn't have to code it myself ;)
I created an extension method to umbraco.presentation.nodeFactory.Node looking like this:
/// <summary>
/// Extension method for Node, finds first parent of provided doctype name
/// </summary>
/// <param name="node">Node to search from</param>
/// <param name="nodeTypeAlias">Doctype to search for</param>
/// <returns>Node</returns>
public static Node GetFirstParent(this Node node, string nodeTypeAlias)
{
try
{
var currentNode = node;
while (node.NodeTypeAlias != nodeTypeAlias && node.Id != 1)
{
node = node.Parent;
}
return node;
}
catch (Exception ex)
{
return null;
}
}
This extension method lives in a class called UmbracoExt in an external class library called Iqit.UmbracoExtensions added as a reference to the project I'm building, which means I can use the code like this:
using Iqit.UmbracoExtensions;
// .. initializing page etc.
var myTypeParent = Node.GetCurrent().GetFirstParent("myType");
Where "mytype" is the doctype alias I'm looking for. If nothing is found, the method returns null, which of course needs to be handled by your code.
Recursive search for property from code
In a .cs codebehind i would like to do the equivalent of, the following xslt
I'm looking at the Parent property of umbraco.presentation.nodeFactory.Node, and am thinking of creating some kind of recursive method call to do it. But I was wondering if there was some method of doing this in the API already. So I didn't have to code it myself ;)
How would you do it?
Regards
Jesper Hauge
FWIW: Here's what I ended up doing:
I created an extension method to umbraco.presentation.nodeFactory.Node looking like this:
This extension method lives in a class called UmbracoExt in an external class library called Iqit.UmbracoExtensions added as a reference to the project I'm building, which means I can use the code like this:
Where "mytype" is the doctype alias I'm looking for. If nothing is found, the method returns null, which of course needs to be handled by your code.
Regards
Jesper Hauge
LINQ to Umbraco with have this OOTB ;). I wrote an AncestorOrSelf<T> method which you can do this with.
Sweet - did not think of that, I'll check out LINQ to Umbraco.
Thanks
.Jesper
It's a 4.1 feature, just incase you're not aware :)
is working on a reply...