I can't seem to find any way to get an IPublishedContent in the GatheringNodeData event. The UmbracoContext is null so I can't use the helper.
I have a lot of methods that rely on the IPublishedContent interface and I have to do a lot of manipulation of data before it is stored in the search index. This means I effectively need to duplicate all my code to work with Nodes which is seriously uncool!
Ccertain methods don't work on nodes either, like GetCropUrl() etc so it's really causing me issues.
I think the below is a bit hacky but you could always ensure that you have an Umbraco Context in your event:
// Ensure that we have an umbraco context
// See: http://issues.umbraco.org/issue/U4-5445#comment=67-16121
if (UmbracoContext.Current == null)
{
UmbracoContext.EnsureContext(
context,
ApplicationContext.Current,
new WebSecurity(context, ApplicationContext.Current),
true);
}
// Create a new helper
var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
Sorry. I should have mentioned the context variable is the current request.
I think without have HttpContent.Current you are going to struggle getting UmbracoContext to work. I believe UmbracoContext relies on having a request. Also the GatheringNodeData runs on a separate thread, not the main thread so HttpContent.Current will always be null.
I tried looking for a solution and I can't find one I'm afraid.
Just pay attention because the services are DB lookups and are much more slowly than the IPublishedContent lookups. Because the GatheringNodeData is only triggered when the node is being index, that is probably ok.
If you are interested, here is the solution for v7.3
if (UmbracoContext.Current == null)
{
var dummyHttpContext = new HttpContextWrapper(new HttpContext(new SimpleWorkerRequest("blah.aspx", "", new StringWriter())));
UmbracoContext.EnsureContext(
dummyHttpContext,
ApplicationContext.Current,
new WebSecurity(dummyHttpContext, ApplicationContext.Current),
UmbracoConfig.For.UmbracoSettings(),
UrlProviderResolver.Current.Providers,
false);
}
Life saver - thanks Lee! I was stuck on this with a Merchello project when trying to hook into the GatheringNodeData event and getting underlying exceptions after calling AsProductContent() on the productDisplay instance.
I had problems with the UmbracoContext.Current being null using the solution detailed in the staheri.com link provided by @Veronica, even though that link says it's not an issue for them. Although I did inherit from ApplicationEventHandler rather than IApplicationEventHandler (not sure if that makes much difference).
As a workaround I added the following to the start of the GatheringNodeData event handler (Umbraco 7.2.4, similar to @Lee's v7.3 code, above):
if (UmbracoContext.Current == null)
{
var dummyContext = new HttpContextWrapper(new HttpContext(new SimpleWorkerRequest("/", string.Empty, new StringWriter())));
UmbracoContext.EnsureContext(
dummyContext,
ApplicationContext.Current,
new WebSecurity(dummyContext, ApplicationContext.Current),
false);
}
IPublishedContent and GatheringNodeData
Hello,
I can't seem to find any way to get an IPublishedContent in the GatheringNodeData event. The UmbracoContext is null so I can't use the helper.
I have a lot of methods that rely on the IPublishedContent interface and I have to do a lot of manipulation of data before it is stored in the search index. This means I effectively need to duplicate all my code to work with Nodes which is seriously uncool!
Ccertain methods don't work on nodes either, like GetCropUrl() etc so it's really causing me issues.
Is there a way?
Hi Valerie,
I think the below is a bit hacky but you could always ensure that you have an Umbraco Context in your event:
Thanks, Dan.
Hi Dan,
What is "context" in this example? I tried new HttpContextWrapper(HttpContext.Current) but HttpContext.Current is null.
Sorry. I should have mentioned the context variable is the current request.
I think without have HttpContent.Current you are going to struggle getting UmbracoContext to work. I believe UmbracoContext relies on having a request. Also the GatheringNodeData runs on a separate thread, not the main thread so HttpContent.Current will always be null.
I tried looking for a solution and I can't find one I'm afraid.
Thanks, Dan.
Thanks anyway Dan!
As stated by Shannon:
You can use the Services if you need to look up properties from other nodes:
Unfortunately the content service returns IContent which yet another different interface :(
I know, but you can do already a lot with it.
Just pay attention because the services are DB lookups and are much more slowly than the IPublishedContent lookups. Because the GatheringNodeData is only triggered when the node is being index, that is probably ok.
I stumbled over the IPublishedContent by doing:
var context = new HttpContext(new HttpRequest("", "http://mysite.local/", ""), new HttpResponse(null));
UmbracoContext.EnsureContext(new HttpContextWrapper(context), ApplicationContext.Current);
var content = new UmbracoHelper(UmbracoContext.Current).TypedContent(e.NodeId);
Will there be negative implications to doing this with a mock httpcontext? It does indeed return the correct IPublishedContent object.
If you are interested, here is the solution for v7.3
Life saver - thanks Lee! I was stuck on this with a Merchello project when trying to hook into the
GatheringNodeData
event and getting underlying exceptions after callingAsProductContent()
on theproductDisplay
instance.Hi Valerie,
This link http://staheri.com/my-blog/2015/march/custom-examine-indexing-using-umbraco-cache/ helped me when I had the same requirement.
It works a treat and seriously speeds up my code when setting up data for my custom index.
HTH
Ver
I had problems with the UmbracoContext.Current being null using the solution detailed in the staheri.com link provided by @Veronica, even though that link says it's not an issue for them. Although I did inherit from ApplicationEventHandler rather than IApplicationEventHandler (not sure if that makes much difference).
As a workaround I added the following to the start of the GatheringNodeData event handler (Umbraco 7.2.4, similar to @Lee's v7.3 code, above):
More info here
is working on a reply...