TackleMcClean ๐
06/07/2024, 9:40 AMContentSingleAtXPath
is being deprecated.
For example, we get our global settings node as such:
var globalSettings = (GlobalSettings)_umbracoHelper.ContentSingleAtXPath("//globalsettings");
How can we get the same published content but adhering to current conventions? Looking at the documentation there is a method Content
that takes a guid
, but we do not wish to rely on a hard coded guid for this scenario.
We need to base this on what the slug is named within the backoffice.
And yes, we know there might be other approaches that do not rely on the slug to solve this, but this is our current scenario and not the only instance we need to cater to.Sebastiaan
06/07/2024, 10:34 AMSebastiaan
06/07/2024, 10:35 AMSebastiaan
06/07/2024, 10:37 AM_umbracoHelper.GetContentAtRoot().First(x => x.Alias == "globalsettings")
(hardcoding an alias instead of a Guid.. up to you).
Untested code, don't expect it to work without some tweaking ๐D_Inventor
06/07/2024, 10:50 AMGetByContentType( ... )
to replace that specific xpath query.Sebastiaan
06/07/2024, 10:55 AMTackleMcClean ๐
06/07/2024, 12:07 PMD_Inventor
06/07/2024, 12:07 PMIUmbracoContext
thoughD_Inventor
06/07/2024, 12:08 PMTackleMcClean ๐
06/07/2024, 12:08 PMvar globalSettings =
(GlobalSettings)(from node in _umbracoHelper.ContentAtRoot()
where node.ContentType.Alias == "globalSettings"
select node).First();
TackleMcClean ๐
06/07/2024, 12:10 PMSebastiaan
06/07/2024, 12:31 PMcsharp
var globalSettings = _umbracoHelper
.ContentAtRoot()
.First(x => x.ContentType.Alias == "globalSettings")
as GlobalSettings;
Matthew
06/08/2024, 6:38 PMGlobalSettings.ModelTypeAlias
, though
umbracoHelper.ContentAtRoot().OfType<GlobalSettings>().First()
would be my go-to solutionMike Chambers
06/10/2024, 9:20 AM//doctype
xpath of get the first one anywhere in the tree without me knowing where it is and so having to go traversing. I guess we fallback to examine for that? or is linq traversal now just a quick?D_Inventor
06/10/2024, 12:36 PMGetByContentType
on the UmbracoContext object does the same as that specific xpath query as far as I know.Mike Chambers
06/10/2024, 3:43 PMpublic virtual IEnumerable<IPublishedContent> GetByContentType(IPublishedContentType contentType) =>
// this is probably not super-efficient, but works
// some cache implementation may want to override it, though
GetAtRoot()
.SelectMany(x => x.DescendantsOrSelf(_variationContextAccessor!))
.Where(x => x.ContentType.Id == contentType.Id);
First comment says it all ๐ ... and it was the ContentSingleAtXPath("//doctype")
.. I guess is the same as GetByContentType().FirstorDefault()
though would this enumerate the entire content tree rather than stop once it found the first item?
Also I've come a cropper over that first here isn't always equivalent to first in xpath... think it traverses down each sub tree to the very bottom descendant before moving on.. where as the xpath traverses siblings first, before then moving onto the next tier of descendants? But might be mistaken.... ๐D_Inventor
06/11/2024, 6:03 AM.Descendants
did a depth-first search instead of breadth-first