Copied to clipboard

Flag this post as spam?

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


  • shufflemoomin 37 posts 36 karma points
    Jul 07, 2009 @ 14:13
    shufflemoomin
    0

    List all child nodes using API?

    Hi everyone,

    I'm kinda new to using user controls with Umbraco and usually use XSLT.

    I have a user control that I need to return all the child nodes and sometimes also grandchildren nodes for a specifc node of which I know the ID. I would prefer to add these to an array using a loop, but I'm not really sure how to use the API to get the data.

    I'm using Umbraco 4 and coding in C#.

    Can anyone point me in the right direction?

    Thank you very much

    Shufflemoomin

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Jul 07, 2009 @ 14:21
    Dirk De Grave
    0

    Hi,

     

    It's no different from using the umbraco library methods from within xslt...

    Even in your user control, you could for example use umbraco.library.GetXmlNodeByXPath(). Just need to know what xpath query to use and you're set.

    For listing children of a specific node, you'd probably use //node [@id = id]/node or //node [@id = id]/node

    For listing grandchildren of a specific node, you'd  //node [@id = id]//node or //node [@id = id]//node

     

    Hope this helps.

     

    Regards,

    /Dirk

  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Jul 07, 2009 @ 14:43
    Peter Gregory
    7

    Hi using the pure API and no XSLT you would do the following.

    to get the children on your known node you would do the following (this is the long hand way so you can see all the parts easily

    umbraco.presentation.nodeFactory.Node node = new umbraco.presentation.nodeFactory.Node(YOURNODEID); 
    umbraco.presentation.nodeFactory.Nodes childrenNodes = node.children;

    if you were then going to bind this to a say repeater just bind as usual

    yourRepeater.DataSource = childrenNodes;
    yourRepeater.DataBind();

    then in the code fron on the repeater to get the properties of the node you would do the following

    <asp:Repeater ID="yourRepeater" runat="server">
        <HeaderTemplate><ul></HeaderTemplate>
        <ItemTemplate><li><a href="<%# (Container.DataItem as Node).Url %>"><%# Eval("Name") %></a>
        <p><%# (Container.DataItem as Node).GetProperty("YourCustomPropertyAlias").Value %></p>
        </li></ItemTemplate>
        <FooterTemplate></ul></FooterTemplate>
    </asp:Repeater>

    Hope that helps!

    Peter

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jul 07, 2009 @ 14:51
    Aaron Powell
    2

    If you want the ID of the current page to pass to the constructor you can get it via the UmbracoContext:

    int currentNodeId = umbraco.presentation.UmbracoContext.Current.PageId.Value;

     

  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Jul 07, 2009 @ 14:57
    Peter Gregory
    0

    And if you want to get the current node for the current page:

    umbraco.presentation.nodeFactory.Node current = Node.GetCurrent();

     

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jul 07, 2009 @ 15:07
    Aaron Powell
    0

    I may obsolete that method and push it into the UmbracoContext, there's so many ways to do this stuff that I'd like to see it centralized around the context

  • James King 11 posts 20 karma points
    Jul 08, 2009 @ 19:32
    James King
    0

    I have referenced umbraco.dll and receive the commands like umbraco.presentation etc then when I get to the current part it cannot find current. Do I need one of those <%@ register things at the top of the page? At the moment I have <#%@ assembly="umbraco" %> but doesn't change anything.

    Thanks

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jul 09, 2009 @ 05:23
    Aaron Powell
    0

    The current part of what, the UmbracoContext? UmbracoContext was added in Umbraco 4.0 and is in the umbraco.presentation namespace in the umbraco.dll assembly.

    Maybe it'd help if you posted your code thus far and we can let you know where you're going wrong

  • Chris Houston 535 posts 980 karma points MVP admin c-trib
    Jul 27, 2009 @ 15:05
    Chris Houston
    1

    On the example given by Peter above it should read:

    umbraco.presentation.nodeFactory.Nodes childrenNodes = node.Children;

    I.e. the "Children" for node.Children should be a capital C...

    Once I changed that, it did exactly what I needed, thanks Peter :)

    Cheers,

    Chris

     

  • jigar 170 posts 233 karma points
    Jul 28, 2009 @ 10:41
    jigar
    0

    How can i chagne childrenNodes to datatable in following code snipet.

       umbraco.presentation.nodeFactory.Node node = new umbraco.presentation.nodeFactory.Node(1071);
       umbraco.presentation.nodeFactory.Nodes childrenNodes = node.Children;

    Thanks

    JIgar

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jul 28, 2009 @ 11:17
    Aaron Powell
    0
    new umbraco.businesslogic.web.Document(1071).Children;
  • Richard Soeteman 4045 posts 12898 karma points MVP 2x
    Jul 28, 2009 @ 11:22
    Richard Soeteman
    0

    Duplicate question from JIgar. See the Datatable snippet on this post

Please Sign in or register to post replies

Write your reply to:

Draft