Copied to clipboard

Flag this post as spam?

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


  • Valerie 67 posts 163 karma points
    Nov 06, 2014 @ 10:20
    Valerie
    0

    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?

  • Dan Lister 416 posts 1974 karma points c-trib
    Nov 06, 2014 @ 11:23
    Dan Lister
    0

    Hi Valerie,

    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);
    

    Thanks, Dan.

  • Valerie 67 posts 163 karma points
    Nov 06, 2014 @ 11:30
    Valerie
    0

    Hi Dan,

    What is "context" in this example? I tried new HttpContextWrapper(HttpContext.Current) but HttpContext.Current is null.

  • Dan Lister 416 posts 1974 karma points c-trib
    Nov 06, 2014 @ 11:52
    Dan Lister
    0

    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.

  • Valerie 67 posts 163 karma points
    Nov 06, 2014 @ 11:55
    Valerie
    0

    Thanks anyway Dan!

  • Damiaan 442 posts 1301 karma points MVP 6x c-trib
    Nov 06, 2014 @ 15:03
    Damiaan
    0

    As stated by Shannon:

    UmbracoContext.Current is set during the init of a web request so if you are running outside of a web request.. like with examine it is probably running async, so it will be null. https://groups.google.com/forum/#!topic/umbraco-dev/-xto6IIPXrA

    You can use the Services if you need to look up properties from other nodes:

    var contentService = ApplicationContext.Current.Services.ContentService;
    
  • Valerie 67 posts 163 karma points
    Nov 06, 2014 @ 15:11
    Valerie
    0

    Unfortunately the content service returns IContent which yet another different interface :(

  • Damiaan 442 posts 1301 karma points MVP 6x c-trib
    Nov 06, 2014 @ 15:25
    Damiaan
    0

    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.

  • Valerie 67 posts 163 karma points
    Jan 13, 2015 @ 10:50
    Valerie
    1

    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.

  • Lee 1130 posts 3088 karma points
    Sep 30, 2015 @ 19:36
    Lee
    5

    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);
            }
    
  • Simon Dingley 1470 posts 3427 karma points c-trib
    Jul 28, 2016 @ 12:28
    Simon Dingley
    0

    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.

  • Veronica Burd 76 posts 201 karma points
    Oct 01, 2015 @ 06:54
    Veronica Burd
    1

    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

  • Mark 255 posts 612 karma points
    Oct 08, 2015 @ 10:41
    Mark
    0

    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);
                    }
    

    More info here

Please Sign in or register to post replies

Write your reply to:

Draft