Copied to clipboard

Flag this post as spam?

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


  • Marco Graziotti 40 posts 166 karma points c-trib
    May 18, 2021 @ 21:38
    Marco Graziotti
    0

    Use "UmbracoHelper" outside the controller, in a "Components" class

    Hi everyone,

    I'm trying to use the "UmbracoHelper" in a "Components" class, inside a notification handler class (here an example of the new notification handler, and here a video).

    In Umbraco 8 was possible to use the static class "Umbraco.Web.Composing.Current.UmbracoHelper" to retrieve published content, for example:

    var root = umbracoHelper.ContentAtRoot().ToList().DescendantsOrSelf<Root>().FirstOrDefault()
    

    I'm trying to get the "UmbracoHelper" class via dependency injection, but it seems that it's not possible to get it. For example, if you add the constructor to this DontShout class, with UmbracoHelper in DI, you will obtain an exception.

    How can I obtain "UmbracoHelper" outside the controller in Umbraco 9?

    Thank you

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    May 19, 2021 @ 06:39
    Andy Butland
    2

    I think here you can inject IPublishedContentQuery, which is what UmbracoHelper defers for the ContentAtRoot() method.

    If it doesn't work injecting directly, try instead injecting IHttpContextAccessor and resolving from:

    IPublishedContentQuery publishedContentQuery = _httpContextAccessor.HttpContext.RequestServices.GetRequiredService<IPublishedContentQuery>();
    

    Not sure if you've got the same issue I found but if you do need the latter approach, I explained a little about the reasons why this latter approach is needed under "Handing Different Lifetimes of Dependencies" here.

    Andy

  • Marco Graziotti 40 posts 166 karma points c-trib
    May 19, 2021 @ 21:53
    Marco Graziotti
    101

    Dear Andy,

    thank you for your reply.

    I have tried to inject IPublishedContentQuery but it seems that is not present in DI, and it returns this exception when I save the page:

    An error occurred

    Cannot resolve 'System.Collections.Generic.IEnumerable'1[Umbraco.Cms.Core.Events.INotificationHandler'1[Umbraco.Cms.Core.Services.Notifications.ContentSavingNotification]]' from root provider because it requires scoped service 'Umbraco.Cms.Core.IPublishedContentQuery'.

    Instead, using the IHttpContextAccessor I was able to get the UmbracoHelper:

    private readonly UmbracoHelper umbracoHelper;
    
    public MyClassCtor(IHttpContextAccessor contextAccessor)
      {
        umbracoHelper = contextAccessor.HttpContext.RequestServices.GetRequiredService<UmbracoHelper>();
      }
    

    Thank you for referencing your blog, the article was useful, and yes I have got your same issue.

    I'm also curious to read something about the available services in DI and the difference between the services, for example what kind of services .AddBackOffice() and .AddWebsite() will make available? And what's the difference between AddBackOfficeCore() and AddUmbracoCore()?

    Thank you

  • Mahdi Shahbazi 4 posts 79 karma points
    Oct 03, 2021 @ 00:30
    Mahdi Shahbazi
    0

    IUmbracoHelperAccessor is another choice to use in constructor.

    private readonly IUmbracoHelperAccessor _umbracoHelperAccessor;
    
    
    
    public class TestClass
    {
            private readonly IUmbracoHelperAccessor _umbracoHelperAccessor;
    
            public TestClass(IUmbracoHelperAccessor umbracoHelperAccessor)
            {
                _umbracoHelperAccessor = umbracoHelperAccessor;
            }
            public void Test()
            {
                if (_umbracoHelperAccessor.TryGetUmbracoHelper(out var umbracoHelper) is false)
                {
                    return;
                }
    
                var rootNode = umbracoHelper.ContentAtRoot().FirstOrDefault();
            }
    }
    
  • andrew shearer 510 posts 659 karma points
    Oct 04, 2021 @ 19:40
    andrew shearer
    0

    this sounds the same question as my earlier thread if that helps https://our.umbraco.com/forum/umbraco-9//106681-umbraco-context-in-viewcomponent-best-practice

  • fatmazayed 41 posts 122 karma points
    Nov 08, 2022 @ 19:29
Please Sign in or register to post replies

Write your reply to:

Draft