So I have to install some other component (UQuery) in order to select a node in the content tree? Am I understanding that right? There is no simple out of the box way of doing it?
Your xpath example does not start from root node, so how would that work if node I am looking for os not below current node?
Yes, your last example did return the Data node although if another node with the name "Data" existed somewhere in tree then that would be returned as well right?
The above should do the trick - GetNodesByName() will be converted into an XPath expression so should be fast, but it will find all Data nodes thoughout the site and take the first one, to ensure you get a node called Data that sits immediately beneath the root you could try:
Select node by path
I want to select the Data node in umbraco tree below using the path.
Content
Data
I could be anywhere in the three.
I have tried among other things something like Model.XPath("/Data").FirstOrDefault() but that does not work.
Hey Barry,
You could get that using Model.AncestorOrSelf(). See how that goes
Could you be more specific. My current node might not be a decendant.
One trick I use is to put a content picker property on the Model.AncestorOrSelf() Node which can be used to point to the Data Node
eg: Property Alias: DataRepository
and then use
var repo = Model.NodeById(Model.AncestorOrSelf().DataRepository)
Will that get you working?
Hi Barry,
If you want to select all instances of 'Data' nodes how about:
or
HTH,
Hendy
@Hendy Racher
So I have to install some other component (UQuery) in order to select a node in the content tree? Am I understanding that right? There is no simple out of the box way of doing it?
Your xpath example does not start from root node, so how would that work if node I am looking for os not below current node?
Hi Barry,
uQuery is available in Umbraco 4.8, or if you're using an older version then uComponents would be required.
The above XPath will start at the root (to start from the current node you could use the $currentPage token in the XPath expression).
I'm using 4.8, so I'll give it a go.
'umbraco.MacroEngines.BaseContext.Node' is a 'property' but is used like a 'type'
If I add:
using uComponents.Core;
The type or namespace name 'uComponents' could not be found (are you missing a using directive or an assembly reference?)
@using umbraco.MacroEngines;
@using umbraco;
@{
var dataNode = uQuery.GetNodesByXPath("//Data[@isDoc]").FirstOrDefault();
if (dataNode == null) {
node not found
}
else
{
node found
}
}
This outputs: node not found. The Data node is located under "Content".
NOTE: after I edited this post it changed changed my h3 tags but you get the idea.
Hi Barry, what's the alias of the 'Data' node ? is it 'Data' or 'data' - are there 'Data' xml nodes in your /App_Data/umbraco.config file ?
First two lines from umbraco.config look like this:
<root id="-1">
<DocDataFolder id="1049" parentID="-1" level="1" writerID="0" creatorID="0" nodeType="1048" template="0" sortOrder="3" createDate="2012-07-31T16:09:12" updateDate="2012-07-31T16:13:21" nodeName="Data" urlName="data" writerName="admin" creatorName="admin" path="-1,1049" isDoc="">
@uQuery.GetRootNode().Children[0].Name; // THIS ONE RETURNS "Data". But this is too hardcoded.
@uQuery.GetRootNode().Children.Where("Name == \"Data\""); // THIS ONE CRASHES SCRIPT
@uQuery.GetRootNode().Children.Where("x => x.Name == \"Data\""); // THIS ONE CRASHES SCRIPT
@uQuery.GetRootNode().Children.Where("x => x.Name == 'Data'"); // THIS ONE CRASHES SCRIPT
Arrh, that'd explain why the XPath expression didn't return anything - from the above, the node type is "DocDataFolder" which has a name of "Data".
Do you want to retrieve all nodes called "Data" or all nodes using the document type "DocDataFolder" ?
BTW, in the above example where the script crashes, this is because the Linq Where method will expect a lambda expression rather than a string.
Does the following help at all ?
I want to retrieve the node by the name "Data" located under the root node. I know there is only one under content.
Yes, your last example did return the Data node although if another node with the name "Data" existed somewhere in tree then that would be returned as well right?
The above should do the trick - GetNodesByName() will be converted into an XPath expression so should be fast, but it will find all Data nodes thoughout the site and take the first one, to ensure you get a node called Data that sits immediately beneath the root you could try:
(or use a suitable XPath expression)
Both
var temp = uQuery.GetRootNode().GetChildNode().Where(x => x.Name == "Data").FirstOrDefault();
And
var temp = uQuery.GetRootNode().GetChildNode().Where("Name == \"Data\"").FirstOrDefault();
AND
var temp = uQuery.GetRootNode().Children.Where(x => x.Name == "Data").FirstOrDefault();
Crashes script.
sorry, a typo, should be GetChildNodes()
YES, that worked. In the meantime I also got this one to work:
var temp = uQuery.GetNodesByXPath("//DocDataFolder[@nodeName='Data']").FirstOrDefault();
I like your method better though. Thank you for your help!
is working on a reply...