I'm trying to use the Umbraco.RenderTemplate function in a Examine GatheringNodeData event. I'm trying doing do this so I can parse the output of the page and store the content in the examin index. Otherwise I would end up writing a lot of indexing code for all my bits and pieces for example archetype and grid property editors.
The Umbraco version I am using is 7.1.9 and I use RouteHijacking
This is the code that I have :
Register GatheringNodeData event :
namespace MyProject.Events
{
public class Registration : ApplicationEventHandler
{
/// <summary>
/// The application started event handler
/// </summary>
/// <param name="umbracoApplication">
/// The umbraco application.
/// </param>
/// <param name="applicationContext">
/// The application context.
/// </param>
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
var helper = new UmbracoHelper(UmbracoContext.Current);
ExamineManager.Instance.IndexProviderCollection["SiteIndexer"].GatheringNodeData
+= (sender, e) => ExamineEvents.GatheringContentData(sender, e, helper);
base.ApplicationStarted(umbracoApplication, applicationContext);
}
}
}
GatheringNodeData event handler :
namespace MyProject.Events
{
internal static class ExamineEvents
{
public static void GatheringContentData(object sender, IndexingNodeDataEventArgs e, UmbracoHelper helper)
{
int id = e.NodeId;
var content = helper.TypedContent(id);
if (content != null)
{
// code removed to add some custom fields to index
var output = helper.RenderTemplate(id);
// code removed to parse returned html
e.Fields.Add("Content", output.ToString());
}
}
}
}
This is the error I get on the RenderTemplate function :
Error rendering template with id 2645: 'System.InvalidOperationException: An error occurred when trying to create a controller of type 'MyProject.Controllers.DefaultController'. Make sure that the controller has a parameterless public constructor. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentNullException: Value cannot be null.
Parameter name: umbracoContext
at Umbraco.Web.Mvc.RenderMvcController..ctor()
at MyProject.DefaultController..ctor()
--- End of inner exception stack trace ---
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
--- End of inner exception stack trace ---
at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
at Umbraco.Web.Mvc.RenderControllerFactory.CreateController(RequestContext requestContext, String controllerName)
at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext)
at Umbraco.Web.Templates.TemplateRenderer.RenderUmbracoRequestToString(RequestContext requestContext)
at Umbraco.Web.Templates.TemplateRenderer.ExecuteTemplateRendering(TextWriter sw, PublishedContentRequest contentRequest)
at Umbraco.Web.Templates.TemplateRenderer.Render(StringWriter writer)
at Umbraco.Web.UmbracoHelper.RenderTemplate(Int32 pageId, Nullable`1 altTemplateId)'
Its because this happens on a background thread that has no HttpContext, this will basically just never work unless you new-up a new HttpContext with everything that might be required in a request which will be difficult. I don't think this is a very good way to achieve what you want and it will be awfully bad for performance.
I understand the idea, you're basically attempting to index what Google would see. Even if you attempt to go down this route, you will have to create your own lucene analyzers to deal with the data since it will be full of HTML and information that you don't want to index.
You're much better off just parsing the JSON for these property editors into separate fields. Also note that the DocumentWriting event in Examine gives you direct access to underlying Lucene Document instance so you can write whatever data you want there and create any fields you like without having to declare them in your examine config.
Umbraco.RenderTemplate Error
Hi all,
I'm trying to use the Umbraco.RenderTemplate function in a Examine GatheringNodeData event. I'm trying doing do this so I can parse the output of the page and store the content in the examin index. Otherwise I would end up writing a lot of indexing code for all my bits and pieces for example archetype and grid property editors.
The Umbraco version I am using is 7.1.9 and I use RouteHijacking
This is the code that I have :
Register GatheringNodeData event :
GatheringNodeData event handler :
This is the error I get on the RenderTemplate function :
Anybody got an idea how to solve this ?
Dave
Its because this happens on a background thread that has no HttpContext, this will basically just never work unless you new-up a new HttpContext with everything that might be required in a request which will be difficult. I don't think this is a very good way to achieve what you want and it will be awfully bad for performance.
I understand the idea, you're basically attempting to index what Google would see. Even if you attempt to go down this route, you will have to create your own lucene analyzers to deal with the data since it will be full of HTML and information that you don't want to index.
You're much better off just parsing the JSON for these property editors into separate fields. Also note that the DocumentWriting event in Examine gives you direct access to underlying Lucene Document instance so you can write whatever data you want there and create any fields you like without having to declare them in your examine config.
is working on a reply...