I often use xpath to retrieve specific content nodes. Except in 9 I can't seem to get it working.
The functionality is there but it always returns null
using (var cref = _contextFactory.EnsureUmbracoContext())
{
var something = cref.UmbracoContext.Content.GetSingleByXPath("root/homepage");
}
I also can't find the umbraco.config file anywhere either so I'm not sure if support has been dropped or it's just stored somewhere else now? It used to be stored in the App_Data folder which I think is no longer used by the default install of 9.
Mostly you don't need to use XPath in v8 and v9 and because of the new caching layer the LINQ querying is faster. XPath is still supported because it is used a few places like in MNTP property editor, but there is no real XML document, so it is a representation to use XPath queries.
So in your case I would use the following:
using (var cref = _contextFactory.EnsureUmbracoContext())
{
var cache = cref.UmbracoContext.Content;
var homePage = cache.GetAtRoot().FirstOrDefault() as HomePage;
}
XPath no longer supported?
Hey,
I often use xpath to retrieve specific content nodes. Except in 9 I can't seem to get it working.
The functionality is there but it always returns null
I also can't find the umbraco.config file anywhere either so I'm not sure if support has been dropped or it's just stored somewhere else now? It used to be stored in the App_Data folder which I think is no longer used by the default install of 9.
This is using the release candidate of 9
Cheers, Mark V
Hi Mark,
"umbraco.config" was replaced in Umbraco v8 by the "NuCache". v9 does not bring anything new, just a port to .Net 5 (.Net Core).
Are you sure the xpath is correct? I would assume it should start with
"/"
, like"/root/homepage"
Hey Søren,
Yeah I'm thinking of 7 with the umbraco.config, whoops
The query should be correct as I'm trying to migrate code from a site using umbraco 8 and the content is the same.
Although It wasn't working yesterday I booted it up this morning and it's working now! 🤷♂️ Not sure what I did differently?
Cheers, Mark V
Hi Mark
Mostly you don't need to use XPath in v8 and v9 and because of the new caching layer the LINQ querying is faster. XPath is still supported because it is used a few places like in MNTP property editor, but there is no real XML document, so it is a representation to use XPath queries.
So in your case I would use the following:
Hey Bjarne,
Thank you, I'll avoid using xpath for the future 👍
Cheers, Mark V
is working on a reply...