Copied to clipboard

Flag this post as spam?

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


  • Tom Maton 387 posts 660 karma points
    Jul 16, 2010 @ 14:31
    Tom Maton
    0

    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

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Jul 16, 2010 @ 15:07
    Ismail Mayat
    0

    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 

  • Folkert 82 posts 212 karma points
    Jul 16, 2010 @ 15:34
    Folkert
    1

    You can use this snippet, copied from someone else but very usefull:

     /******* LINQ QUERYABLE *******/
            /// <summary>
            /// LINQ examples:
            /// ---Decendants of a certain doctype---
            /// node.AllDescendants().Where(n => n.NodeTypeAlias == "myNodeTypeAlias");
            /// ---Imediate children---
            /// node.AllDescendants().Where(n => (node.Depth() - n.Depth()) == 1);
            /// ---Not hidden---
            /// node.AllDescendants().Where(n => !n.GetPropertyAsBoolean("umbNaviHide"));
            /// </summary>
            /// <param name="node"></param>
            /// <returns></returns>
            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;
                }
            }
  • Tom Maton 387 posts 660 karma points
    Jul 16, 2010 @ 17:05
    Tom Maton
    0

    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

  • Folkert 82 posts 212 karma points
    Jul 16, 2010 @ 17:12
    Folkert
    0

    Samle is in the comments:

    node.AllDescendants().Where(n => n.NodeTypeAlias == "myNodeTypeAlias");

    It returns a collection of nodes (IEnumerable<Node>).

    You can setup somethinglike

    var nodes = node.AllDescendants().Where(n => n.NodeTypeAlias == "myNodeTypeAlias");
    foreach(Node node in nodes)
    {
    // do your stuff here...
    }

    Or bind de nodescollection to a databound control:

    var nodes = node.AllDescendants().Where(n => n.NodeTypeAlias == "myNodeTypeAlias");
    this.repeaterControl.DataSource = nodes.ToList();
    this.repeaterControl.DataBind();

    <ul>
    <asp:repeater ID="repeaterControl" runat="server">
    <itemtemplate>
    <li><%# Eval("nodeName") %></li>
    </itemtemplate>
    </asp:repeater>
    </ul>
  • Tom W 39 posts 96 karma points
    Sep 03, 2010 @ 14:01
    Tom W
    1

    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#!

     

    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!

    Cheers,

    Tom

  • Adam Maidment 54 posts 163 karma points
    Sep 24, 2013 @ 11:06
    Adam Maidment
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft