Copied to clipboard

Flag this post as spam?

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


  • Nauman 98 posts 118 karma points
    Mar 04, 2010 @ 12:21
    Nauman
    0

    How to get topnodes

    How can I get topnodes for following nodes in content.

    Site1

    • index
      about

    Site2

    • index
    • about

     

    I want to get "Site1" and "Site2" in one of my own script?

    Nauman

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Mar 04, 2010 @ 12:49
    Thomas Höhler
    2

    If you are using xslt try this XPAth expression:

    "$currentPage/ancestor-or-self::node [@level=1]"

    if you want to do this with the NodeFactory try this:

    var myRootNode = new umbraco.presentation.nodeFactory.Node(int.Parse(umbraco.presentation.nodeFactory.Node.GetCurrent().Path.Split(',')[1]));

    hth, Thomas

  • Tim 225 posts 690 karma points
    Mar 04, 2010 @ 12:56
    Tim
    0

    Are you using asn XSLT macro or a Usercontrol?

    I'll assume you are using XSLT and give this example for now.

    How to get them dependants a little on where you are in the tree when you ask for them

    The most flexible way of grabbing them would be to use this xPath:

    <xsl:variable name="topNode" select="$currentPage/ancestor-or-self::node [@level=1]/node" />

    This assumes the Site1 etc nodes are at level 1 in the hierarchy. If they are deeper then you can just adjust the @level param accordingly.

    The xPath above looks up the node tree for the current page until it finds an ancestor at the specified level. You can do further filtering in the xpath if required.

    Here's some more info on xpath axes

    T

     

     

  • Tim 225 posts 690 karma points
    Mar 04, 2010 @ 12:57
    Tim
    0

    Rats!

    Thomas you beat me too it, I must be more succinct in my answers ;)

    T

  • Nauman 98 posts 118 karma points
    Mar 04, 2010 @ 13:11
    Nauman
    0

    Thomas and Tim, nice work guys.

    I am using usercontrol and nodefactory in my own section i have created in umbraco. I want to populate a drop down with all the top nodes. Please note that I am not in the tree and working outside the node trees

    var myRootNode = new umbraco.presentation.nodeFactory.Node(int.Parse(umbraco.presentation.nodeFactory.Node.GetCurrent().Path.Split(',')[1]));

    sl.DataSource = myRootNode;
    sl.DataBind(); // where sl is my dropdown list

    Can you please correct the above for me?

    Cheers

    Nauman

     

  • Jesper Hauge 298 posts 487 karma points c-trib
    Mar 04, 2010 @ 13:22
    Jesper Hauge
    1

    If you're going to do this often from code, I'd recommend this extension method i frequently use, it searches the parents by doctype, not level, but you could probably 

    using System;
    using umbraco.presentation.nodeFactory;
    
    namespace Iqit.Umbraco.Extensions
    {
        public static class UmbracoExt
        {
            /// <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 (currentNode.NodeTypeAlias != nodeTypeAlias 
                           && currentNode.Id != 1)
                    {
                        currentNode = currentNode.Parent;
                    }
                    return currentNode;
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
    }

    Put this in a class library, compile it and reference the dll in you umbraco project. Now you can use it in user controls like this:

    using umbraco.presentation.nodeFactory;
    using Iqit.Umbraco.Extensions;
    
    namespace YourProject.usercontrols
    {
        private readonly Node _currentNode = Node.GetCurrent();
    
        protected void Page_Load(object sender, EventArgs e)
        {
            var rootParent = _currentNode.GetFirstParentOfType("yourDoctype");
        }
    }

    Regards
    Jesper Hauge

  • Nauman 98 posts 118 karma points
    Mar 04, 2010 @ 13:28
    Nauman
    0

    Jesper, nice idea. But you see I want to get all the top nodes and as far as I can see your code gets just one top level node. I want to get all. Any other idea guys?

  • Tim 225 posts 690 karma points
    Mar 04, 2010 @ 13:30
    Tim
    0

    In that case you'll need to specify a node id to start from:

    assume you have a root node with an id of 1123

    var myRootNode = new umbraco.presentation.nodeFactory.Node(1123);

    sl.DataSource = myRootNode.ChildrenAsTable();
    sl.DataBind();

    That grabs all the children of the specified node as a datatbable which you can databind to.

    T

  • Tim 225 posts 690 karma points
    Mar 04, 2010 @ 13:40
    Tim
    0

    OK

    How about this:

    var rootNodes = new umbraco.presentation.nodeFactory.Node(-1).ChildrenAsTable();
    
    sl.DataSource = rootNodes;
    sl.DataBind();

    Get's all children of the root of the site.

    T

  • Nauman 98 posts 118 karma points
    Mar 04, 2010 @ 13:42
    Nauman
    0

    Thanks, these are getting all children, how to get all topnodes in the site?

  • Tim 225 posts 690 karma points
    Mar 04, 2010 @ 13:44
    Tim
    0

    My example above gets all the top nodes.

    It gets the children of the Content node id=-1

    T

  • Nauman 98 posts 118 karma points
    Mar 04, 2010 @ 14:12
    Nauman
    0

    Thanks Tim, this works great.

    var rootNodes = new umbraco.presentation.nodeFactory.Node(-1).ChildrenAsTable();

    sl
    .DataSource = rootNodes;
    sl
    .DataBind();

     

    How can i pouplate name as DataTextField and id as DataValueField in drop down.

  • Tim 225 posts 690 karma points
    Mar 04, 2010 @ 15:16
    Tim
    0

    Not tested but this should work

    sl.DataValueField = "Id";
    sl.DataTextField = "NodeName";

    T

  • Tim 225 posts 690 karma points
    Mar 04, 2010 @ 16:11
    Tim
    0

    Just thought I'd tidy up the code sample:

    var rootNodes = new umbraco.presentation.nodeFactory.Node(-1).ChildrenAsTable();

    sl
    .DataSource = rootNodes;
    sl.DataValueField = "Id";
    sl
    .DataTextField = "NodeName";

    sl.DataBind();

    T

  • Tim 225 posts 690 karma points
    Mar 04, 2010 @ 16:12
    Tim
    0

    Don't forget to accept the answer if it works.

    T

  • Nauman 98 posts 118 karma points
    Mar 05, 2010 @ 06:24
    Nauman
    0

    Thanks Tim, it works, you offered a great help, thanks again.

    Cheers

    Nauman

Please Sign in or register to post replies

Write your reply to:

Draft