Copied to clipboard

Flag this post as spam?

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


  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Oct 09, 2019 @ 20:37
    Ismail Mayat
    0

    Umbraco helper in a component

    In v7 in examine events you could instantiate an UmbracoHelper.

    In v8 in composition I am trying to inject UmbracoHelper however i get error:

    Umbraco cannot run. See Umbraco's log file for more details.

    -> Umbraco.Core.Exceptions.BootFailedException: Boot failed.

    -> System.NullReferenceException: Object reference not set to an instance of an object.

    I want to use the helper to do RenderTemplate on page being indexed to get the rendered html.

    Is it possible to inject an UmbracoHelper into a composition?

    Regards

    Ismail

  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Oct 10, 2019 @ 05:01
    Shaishav Karnani from digitallymedia.com
    0

    Hi Ismail,

    You can add IUmbracoContextFactory umbracoContextFactory in the Component Constructor and later use it.

    public MyComponent(IUmbracoContextFactory context)
    {
        _context = context;
    }
    
    public void Initialize()
    {
        using (var cref = _context.EnsureUmbracoContext())
        {
            var cache = cref.UmbracoContext.ContentCache;
            var node = cache.GetById(1234);
        }
    }
    

    Hope that helps to solve the issue.

    Cheers,

    Shaishav

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Oct 10, 2019 @ 06:47
    Ismail Mayat
    0

    I already know how to do this. I am not trying to get a content item, I want to use the RenderTemplate method on UmbracoHelper to get the rendered content.

    To be honest I dont think its possible.

  • Steve Megson 151 posts 1024 karma points MVP c-trib
    Oct 10, 2019 @ 07:28
    Steve Megson
    1

    The problem is that creating an UmbracoHelper requires an UmbracoContext, which doesn't exist yet when your component's constructor runs. You can get around this by injecting a Lazy<UmbracoHelper>, and accessing its value once the context is set up:

    private IUmbracoContextFactory _context;
    private Lazy<UmbracoHelper> _helper;
    
    public MyComponent(IUmbracoContextFactory context, Lazy<UmbracoHelper> helper)
    {
        _context = context;
        _helper = helper;
    }
    
    public void Initialize()
    {
        using (var cref = _context.EnsureUmbracoContext())
        {
            _helper.Value.RenderTemplate(1234);
        }
    }
    
  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Oct 10, 2019 @ 09:01
    Ismail Mayat
    0

    Steve,

    So it works in the sense that it runs, however when accessing the helper its null. I just had this from someone on umbraco slack and may try that when i get a chance:

    In Umbraco 8, you can also inject IUmbracoComponentRenderer or ITemplateRenderer, see the implementations: https://github.com/umbraco/Umbraco-CMS/blob/3bfd9b71e290354744d677440983ecd06c5c0788/src/Umbraco.Web/UmbracoComponentRenderer.cs and https://github.com/umbraco/Umbraco-CMS/blob/3bfd9b71e290354744d677440983ecd06c5c0788/src/Umbraco.Web/Templates/TemplateRenderer.cs

  • Steve Megson 151 posts 1024 karma points MVP c-trib
    Oct 10, 2019 @ 10:17
    Steve Megson
    0

    I forgot that you were rendering the template during Examine indexing. I can get an UmbracoHelper in the Initialize method of the component, but not when handling the TransformingIndexValues event.

    Unfortunately I don't think that injecting UmbracoComponentRenderer or TemplateRenderer will help you. You should be able to inject them, but ultimately when you call RenderTemplate it must need to create an UmbracoHelper at some point for the template to use. If we can't create one directly in the event handler, I don't think that RenderTemplate will be able to either.

    I'll see if I can track down what's stopping it from creating an UmbracoHelper.

  • Steve Megson 151 posts 1024 karma points MVP c-trib
    Oct 10, 2019 @ 16:00
    Steve Megson
    0

    I don't think there's an easy way around this. The problem is that HttpContext.Current is null when running in a background thread. Even with an UmbracoHelper available I run into errors within MVC when trying to render the template, and we can't do much about those.

    I'd suggest moving the RenderTemplate into a handler for a suitable event on ContentService, probably TreeChanged. You could store the rendered HTML, perhaps in a dictionary keyed by content ID, and then retrieve it in the Examine event handler.

Please Sign in or register to post replies

Write your reply to:

Draft