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.
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:
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:
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.
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.
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.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
Hi Ismail,
You can add IUmbracoContextFactory umbracoContextFactory in the Component Constructor and later use it.
Hope that helps to solve the issue.
Cheers,
Shaishav
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.
The problem is that creating an
UmbracoHelper
requires anUmbracoContext
, which doesn't exist yet when your component's constructor runs. You can get around this by injecting aLazy<UmbracoHelper>
, and accessing its value once the context is set up: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:
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.
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
, probablyTreeChanged
. You could store the rendered HTML, perhaps in a dictionary keyed by content ID, and then retrieve it in the Examine event handler.is working on a reply...