v15: Obsolete methods and properties: what are the alternatives?
While upgrading from v14 to v15, I received some obsolete warnings on IPublishedContent properties suggesting to use IDocumentNavigationQueryService and I can't find an easy way to replace my code with it.
The official documentation still heavily use these obsolete properties, so it's hard to find how to update the code.
'IPublishedCache.GetAtRoot(string?)' is obsolete: 'Scheduled for removal, use IDocumentNavigationQueryService instead in v17'
See this code, which is used to retrieve the first "blog" node on a site with a single root item: var blogNode = content.GetAtRoot().FirstOrDefault()?.ChildrenOfType("blog")?.FirstOrDefault();
I tried to use IDocumentNavigationQueryService.TryGetRootKeys() but I don't know what to do with a Guid key.
IUmbracoContext methods are always expecting an int id, and no key.
Which service am I missing? Could the obsolete message me updated to include a reference to all services required to obtain an arbitrary content?
I'm with you on this, I'm not 100% sure if the suggested IDocumentNavigationQueryService is the right way as it requires a DB hit - though looks to be cached after first access.
Is this considered more efficient than enumerating the cache tree each time so will overall be better for performance?
I've yet to find a replacement method that will enumerate the cache.
Either way, this is the way I'm using it with the document query service
public IPublishedContent GetHomepage()
{
using var context = _umbracoContextFactory.EnsureUmbracoContext();
if (_documentNavigationQueryService.TryGetRootKeysOfType("homepage", out var rootKeys))
{
return context.UmbracoContext.Content.GetById(rootKeys.First())!;
}
return null;
}
Or
if(_documentNavigationQueryService.TryGetRootKeysOfType("homepage", out var rootKeys))
{
if (_documentNavigationQueryService.TryGetDescendantsKeysOfType(rootKeys.First(), "teamPage", out var descendantsKeys))
{
if (descendantsKeys.Any())
{
currentNode = contentCache.GetById(descendantsKeys.First());
}
}
}
Thank you for this question! I was also relying on GetAtRoot() to collect all content from root. As the alternative hits the database, this may have horrible implication to resolve all root content using database query. It is also not possible to query multiple ids using published content cache.
What is the recommended way to have the same ability?
Actually, I later realised that IPublishedContentQuery is right for most cases
e.g.
_publishedContentQuery.ContentAtRoot();
There's also an IPublishedContentQueryAccessor
e.g.
if (_publishedContentQueryAccessor.TryGetValue(out var ctx))
{
var insightsArticleRoot = ctx
.ContentAtRoot()
.First()
.Descendants()
.FirstOrDefault(x => x.IsDocumentType("insightsLanding"));
v15: Obsolete methods and properties: what are the alternatives?
While upgrading from v14 to v15, I received some obsolete warnings on IPublishedContent properties suggesting to use IDocumentNavigationQueryService and I can't find an easy way to replace my code with it.
The official documentation still heavily use these obsolete properties, so it's hard to find how to update the code.
See this code, which is used to retrieve the first "blog" node on a site with a single root item:
var blogNode = content.GetAtRoot().FirstOrDefault()?.ChildrenOfType("blog")?.FirstOrDefault();
I tried to use
IDocumentNavigationQueryService.TryGetRootKeys()
but I don't know what to do with a Guid key.IUmbracoContext
methods are always expecting an int id, and no key.Which service am I missing? Could the obsolete message me updated to include a reference to all services required to obtain an arbitrary content?
Thanks!
I'm with you on this, I'm not 100% sure if the suggested IDocumentNavigationQueryService is the right way as it requires a DB hit - though looks to be cached after first access.
Is this considered more efficient than enumerating the cache tree each time so will overall be better for performance?
I've yet to find a replacement method that will enumerate the cache.
Either way, this is the way I'm using it with the document query service
Or
Thank you for this question! I was also relying on GetAtRoot() to collect all content from root. As the alternative hits the database, this may have horrible implication to resolve all root content using database query. It is also not possible to query multiple ids using published content cache. What is the recommended way to have the same ability?
Actually, I later realised that IPublishedContentQuery is right for most cases e.g.
There's also an IPublishedContentQueryAccessor
e.g.
Similar to question https://our.umbraco.com/forum/using-umbraco-and-getting-started/115116-v15-finding-content-a-little-lost-in-the-maze
To get a document from a guid you can inject an IDocumentCacheService then call await documentCacheService.GetByKeyAsync(myGuid)
is working on a reply...