Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Thomas B 2 posts 74 karma points
    Nov 18, 2024 @ 10:46
    Thomas B
    2

    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?

    Thanks!

  • Chris Hall 24 posts 97 karma points
    Nov 19, 2024 @ 12:21
    Chris Hall
    1

    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());
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft