Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jesper Hauge 298 posts 486 karma points c-trib
    Jan 15, 2010 @ 11:40
    Jesper Hauge
    0

    Recursive search for property from code

    In a .cs codebehind i would like to do the equivalent of, the following xslt

    <xsl:variable name="parentId" select="$currentPage/ancestor-or-self::node[@nodeTypeAlias='someType'][0]/@id />

    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

  • Jesper Hauge 298 posts 486 karma points c-trib
    Jan 15, 2010 @ 13:02
    Jesper Hauge
    0

    FWIW: Here's what I ended up doing:

    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.

    Regards
    Jesper Hauge

  • Aaron Powell 1708 posts 3044 karma points c-trib
    Jan 15, 2010 @ 23:43
    Aaron Powell
    2

    LINQ to Umbraco with have this OOTB ;). I wrote an AncestorOrSelf<T> method which you can do this with.

  • Jesper Hauge 298 posts 486 karma points c-trib
    Jan 17, 2010 @ 15:30
    Jesper Hauge
    0

    Sweet - did not think of that, I'll check out LINQ to Umbraco.

    Thanks

    .Jesper

  • Aaron Powell 1708 posts 3044 karma points c-trib
    Jan 18, 2010 @ 12:58
    Aaron Powell
    0

    It's a 4.1 feature, just incase you're not aware :)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies