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.
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
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");
}
}
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?
How to get topnodes
How can I get topnodes for following nodes in content.
Site1
about
Site2
I want to get "Site1" and "Site2" in one of my own script?
Nauman
If you are using xslt try this XPAth expression:
if you want to do this with the NodeFactory try this:
hth, Thomas
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:
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
Rats!
Thomas you beat me too it, I must be more succinct in my answers ;)
T
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
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:
Regards
Jesper Hauge
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?
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
OK
How about this:
Get's all children of the root of the site.
T
Thanks, these are getting all children, how to get all topnodes in the site?
My example above gets all the top nodes.
It gets the children of the Content node id=-1
T
Thanks Tim, this works great.
How can i pouplate name as DataTextField and id as DataValueField in drop down.
Not tested but this should work
T
Just thought I'd tidy up the code sample:
T
Don't forget to accept the answer if it works.
T
Thanks Tim, it works, you offered a great help, thanks again.
Cheers
Nauman
is working on a reply...