Copied to clipboard

Flag this post as spam?

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


  • Zakhar 171 posts 397 karma points
    Jul 02, 2012 @ 18:29
    Zakhar
    0

    Traversing nodes in C#

    Hi guys, sorry if such question was already answered here, I can't find the solution.

    I need to get parent's children of particular document type. In razor (4.7.2) I can do it like this:

    @Model.Parent.Descendants("MyDocType")

    I know I can use uQuery to get all nodes of type:

    List<Node> allNodes = uQuery.GetNodesByType("MyDocType");

    That will return all nodes, but I need only parent's children. How can I do it?

    Thanks.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jul 03, 2012 @ 12:38
    Jeroen Breuer
    0

    You can also use that Razor example in C#, but you first need to get the model:

    dynamic model = new umbraco.MacroEngines.DynamicNode(umbraco.NodeFactory.Node.GetCurrent());
    var node = model.Parent.Descendants("MyDocType")

    Jeroen

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Jul 03, 2012 @ 13:02
    Hendy Racher
    0

    Hi Zakhar,

    or how about:

    Node parent = uQuery.GetCurrentNode().Parent;
    List<Node> nodes = parent.GetChildNodesByType("MyDocType");

    Hendy

  • Zakhar 171 posts 397 karma points
    Jul 05, 2012 @ 16:12
    Zakhar
    0

    Hi guys, sorry I didn't reply straight away, was very busy with another project. Thanks for you answers but unfortunately I can't make to work neither of them.

    When I try

    using umbraco.presentation.nodeFactory;
    ...
    Node parent = uQuery.GetCurrentNode().Parent;
    List nodes = parent.GetChildNodesByType("MyDocType");

    It can't build it, I get this error:

    'umbraco.presentation.nodeFactory.Node' does not contain a definition for 'GetChildNodesByType' and no extension method 'GetChildNodesByType' accepting a first argument of type 'umbraco.presentation.nodeFactory.Node' could be found (are you missing a using directive or an assembly reference?)

     

    dynamic model =new umbraco.MacroEngines.DynamicNode(umbraco.NodeFactory.Node.GetCurrent());
    var node = model.Parent.Descendants("MyDocType")

    doesn't build as well. I get:

    The type or namespace name 'DynamicNode' does not exist in the namespace 'umbraco.MacroEngines' (are you missing an assembly reference?)   
    Also it doesn't like model.Parent part :
    One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll?   

    Which dlls do I need to reference? I added umbraco, cms, interfaces, businesslogic to my projects.

    Thanks,

    Zakhar

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Jul 05, 2012 @ 16:51
    Hendy Racher
    0

    Hi Zakhar,

    Sorry, for the uQuery example above, you'll need:

    using uComponents.Core;
    using uComponents.Core.uQueryExtensions; // (this namespace has the extension method GetChildNodesByType)

    HTH,

    Hendy

  • Ernst Utvik 123 posts 235 karma points
    Jul 06, 2012 @ 02:23
    Ernst Utvik
    0
    Node current = new Node(Node.GetCurrent().Id);
    Node parent = new Node(current.Parent.Id);
    Nodes siblings = parent.Children;
    foreach (Node node in siblings){
    if (node.NodeTypeAlias == "somedoctype")
    { do stuff }}

    Not at all tested, but I have done something similar in the past to populate a dropdown ;) uQuery is probably a bit more tidy.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jul 10, 2012 @ 10:13
    Jeroen Breuer
    0

    Hi Zakhar,

    For DynamicNode you need to add a reference to the umbraco.MacroEngines.dll file.

    Jeroen

Please Sign in or register to post replies

Write your reply to:

Draft