Copied to clipboard

Flag this post as spam?

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


  • suzyb 474 posts 932 karma points
    Mar 01, 2011 @ 23:57
    suzyb
    0

    Selecting nodes of certain document type in .net

    Is there a way to select content nodes of a specific document type and their children (different document type) in a .net control.

    I have this structure:

    Program type
    -- program
    -- program
    Program type
    --- program

    And I'd like a list of all the "program type" nodes and their child "program" nodes accessible in my .net control. Can I do this?

  • E.J. Brennan 73 posts 114 karma points
    Mar 02, 2011 @ 00:15
    E.J. Brennan
    1

    Without understanding your structure to much, or what you are trying to do, you might be able to use some of these methods in various combinations to get what you want:

     


       var currentNode = Node.GetCurrent();

    //return all children of the currentNode of the specified type  
     var dt = currentNode.ChildrenAsTable("BlogPost");
    //return all children regardless of type
       var dt = currentNode.ChildrenAsTable();

     

     

    //return all children as a list
       var dt = currentNode.ChildrenAsList();

     

     

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Mar 02, 2011 @ 00:17
    Hendy Racher
    0

    Hi Suzyb,

    There's a helper library called uQuery in uComponents that has just such method to return a collection of nodes by their document type:

    using uComponents.Core;
    using uComponents.Core.uQueryExtensions;

    foreach(Node programTypeNode in uQuery.GetNodesByType("Program type"))
    {
    foreach(Node programNode in programTypeNode.GetChildNodes())
    {
    ...
    }
    }

    HTH,

    Hendy

  • suzyb 474 posts 932 karma points
    Mar 02, 2011 @ 10:44
    suzyb
    0

    The Program Type nodes are the same level as my Home node.  The structure is like this

    Content
    -- Home
    -- Program type
    ------- program

    Program type and program aren't actually pages.  The home page needs to contain a list of all the program types and programs available and I wanted this to be stored in the CMS so it was easy to change.

    To dipslay the list on the home page I use an xslt macro and select the program type nodes using

    $currentPage/ancestor::root//ProgramType

    but I need to do is that in C#.

    I have tried getting the parent node of the current one (which should be the root) but it throws an "Object reference not set to an instance of an object."

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Mar 02, 2011 @ 11:02
    Hendy Racher
    1

    Hi Suzyb,

    There's also a method in uQuery to get nodes by an XPath expression:

    ($currentPage/ancestor::root//ProgramType would work, but by default this method will run from the root, so can use just //ProgramType)

    List<Node> programTypeNodes = uQuery.GetNodesByXPath("//ProgramType");

    or to get the root node directly via the nodeFactory you can create it with the id of -1.

    Node rootNode = new Node(-1);

    HTH,

    Hendy

  • suzyb 474 posts 932 karma points
    Mar 02, 2011 @ 11:54
    suzyb
    1

    Thanks Hendy

    I didn't realise you could select the root node using -1.  Using that, this is what I've ended up with.

    Node rootNode = new Node(-1);
    foreach (Node n in rootNode.Children)
    {
         if (n.NodeTypeAlias == "ProgramType")
         {
             programs.Add(n.Id.ToString(), n.Name);
             foreach (Node child in n.Children)
             {
                  programs.Add(child.Id.ToString(), "----- " + child.Name);
             }
          }
    }
Please Sign in or register to post replies

Write your reply to:

Draft