So I want to use XPath to get a node, and I'm discovered that UmbracoHelper has the Umbraco.TypedContentSingleAtXPath method, but I don't have the #currentPage variable. I want to execute the following xpath:
$currentPage/ancestor-or-self::* [@isDoc and name() = 'HomePage']/descendant-or-self::* [@isDoc and name = 'Job']
My site will be multi language, so I want to go up the tree to the "root" (not the real root, but the language homepage) and get the Job node, with the right language.
I don't believe you can use $currentPage etc in this context.
What you might have to do is something like:
var currentSite = <use some method to get the parent ID of the current site youre in>;
var jobs = Umbraco.TypedContentSingleAtXPath("//* [@id = {0}]/descendant-or-self::* [@isDoc and name = 'Job']", currentSite.Id);
This might have performance implications, but it might be the only way if you want to use XPath.
But my main reason to use xpath ir because of performance... I still don't know the new api, but with Node Class and the uQuery, when I wanted to make some query a litle more performance, I used xPath, and in some sites there was a real diference with this. Maybe with the IPublished class it's direrent? I'm going to make some tests...
Sorry about that, I see what you mean now. You can achieve this using the old NodeFactory and uQuery with something like this:
var jobs = uQuery.GetCurrentNode().GetAncestorByPathLevel(1).GetDescendantNodesByType("Jobs");
And if you're using MVC and want to take advantage of the new IPublishedContent, you can use something like this:
var jobs = Model.Content.AncestorOrSelf("HomePage").Descendants("Job");
Side note - it might be better if you can narrow down where your Jobs doctypes will be and avoid using Descendants - something like this might be best (assuming you always know where the Jobs content will exist):
var jobs = Model.Content.AncestorOrSelf("HomePage").Children.First(x => x.IsDocumentType("Jobs")).Children.Where(x => x.IsDocumentType("Job"));
I see what you mean. Unfortunately I don't think this method supports variables, so you'll probably have to do something like this:
var currentSite = umbraco.NodeFactory.Node.GetCurrent(); // assuming older umbraco version?
var jobs = Umbraco.TypedContentSingleAtXPath("//* [@isDoc][@id = {0}]/descendant-or-self::* [@isDoc and name = 'Job']", currentSite.Id);
var home = Model.Content.AncestorOrSelf("home");
var jobs = Umbraco.TypedContentSingleAtXPath("//* [@isDoc][@id = {0}]/descendant-or-self::* [@isDoc and name = 'Job']", home.Id);
within Surfacecontroller:
var home = SurfaceController.CurrentPage.AncestorOrSelf("home");
var jobs = Umbraco.TypedContentSingleAtXPath("//* [@isDoc][@id = {0}]/descendant-or-self::* [@isDoc and name = 'Job']", home.Id);
Umbraco.TypedContentSingleAtXPath Help
Hi everyone,
So I want to use XPath to get a node, and I'm discovered that UmbracoHelper has the Umbraco.TypedContentSingleAtXPath method, but I don't have the #currentPage variable. I want to execute the following xpath:
My site will be multi language, so I want to go up the tree to the "root" (not the real root, but the language homepage) and get the Job node, with the right language.
How can I get the this?
Hi,
I don't believe you can use $currentPage etc in this context.
What you might have to do is something like:
This might have performance implications, but it might be the only way if you want to use XPath.
-Tom
Hi Tom,
But my main reason to use xpath ir because of performance... I still don't know the new api, but with Node Class and the uQuery, when I wanted to make some query a litle more performance, I used xPath, and in some sites there was a real diference with this. Maybe with the IPublished class it's direrent? I'm going to make some tests...
Thx
Hi Berto,
Sorry about that, I see what you mean now. You can achieve this using the old
NodeFactory
anduQuery
with something like this:var jobs = uQuery.GetCurrentNode().GetAncestorByPathLevel(1).GetDescendantNodesByType("Jobs");
And if you're using MVC and want to take advantage of the new
IPublishedContent
, you can use something like this:var jobs = Model.Content.AncestorOrSelf("HomePage").Descendants("Job");
Side note - it might be better if you can narrow down where your
Jobs
doctypes will be and avoid usingDescendants
- something like this might be best (assuming you always know where theJobs
content will exist):var jobs = Model.Content.AncestorOrSelf("HomePage").Children.First(x => x.IsDocumentType("Jobs")).Children.Where(x => x.IsDocumentType("Job"));
Hope that helps, Tom
Hi Tom,
That is what I'm doing right now, but I wan't to use the xpath. About a year ago I had some performance problems with a site, you can check it here: http://our.umbraco.org/projects/backoffice-extensions/ucomponents/questionssuggestions/31315-Performance-problem-with-uQuery
And the main solution was to either use xpath or GetNodeByType. In this case I just want to use the xpath because of the multi language.
Basically, I just want to know how can I with xPath and the current node, get other contents, or, what is the substitute of the variable $currentPage.
Thanks for the patience and the help!
Berto
Hi Berto,
I see what you mean. Unfortunately I don't think this method supports variables, so you'll probably have to do something like this:
Hope that makes sense, Tom
within "UmbracoTemplatePage" view:
within Surfacecontroller:
is working on a reply...