Copied to clipboard

Flag this post as spam?

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


  • Mary 15 posts 123 karma points
    Dec 09, 2015 @ 11:33
    Mary
    0

    UmbracoContext.Current not available in RenderTask

    Hi all

    I try to process the content of my newsletter using render task.

    While the UmbracoContext.Current in the class RenderTask is available, within the function ProcessUniqueItem(RenderResult renderResult, RenderTaskUniqueItemParameters parameters) the UmbracoContext.Current is null.

    Because I process each unique item and render it very personalized, I need GetPropertyValue therefore the current UmbracoContext is necessary.

    Is there a workaround to get and set the current UmbracoContext for this functionality?

    Thanks in advance.

  • Markus Johansson 1936 posts 5864 karma points MVP 2x c-trib
    Dec 09, 2015 @ 13:02
    Markus Johansson
    0

    Hi!

    We're aware of this and I have a work around in another project - I'll share some more info during the afternoon.

  • Markus Johansson 1936 posts 5864 karma points MVP 2x c-trib
    Dec 09, 2015 @ 17:59
    Markus Johansson
    0

    Hi!

    Just to make sure that you understand =D This is a hack - but it should work =D You need to create an event-handler that move over the references to the sending thread. This is how it might look. We will create a better implementation i the future but this is what I got at the moment.

     public class AppStart : ApplicationEventHandler
        {
            public static UmbracoContext UmbracoContext;
            public static ApplicationContext AppContext;
            public static HttpContextBase CurrentHttpContextBase;
            public static System.Uri HostAndProtocol; 
    
            protected override void ApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
    
                base.ApplicationInitialized(umbracoApplication, applicationContext);
            }
    
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
    
    
                SendNewsletterService.SendningInitializing += (sender, args) =>
                {
                    UmbracoContext = Umbraco.Web.UmbracoContext.Current;
                    HostAndProtocol = RenderTask.ProtocolAndHost;
                    AppContext = ApplicationContext.Current;
                    CurrentHttpContextBase = new HttpContextWrapper(HttpContext.Current);
    
                    UmbracoContext.EnsureContext(CurrentHttpContextBase, AppContext);
                };
    
                base.ApplicationStarted(umbracoApplication, applicationContext);
            }
    
        }
    
  • Mary 15 posts 123 karma points
    Dec 10, 2015 @ 10:24
    Mary
    0

    Hi!

    Thanks for your Codesnippet. I implemented it in my Code but unfortunately the UmbracoContext.Current is in the method ProcessUniqueItem still null. So GetPropertyValue for the objects below has no value as well.

    public class NewsletterRender : RenderTask
    {
        private static UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);
    
    public override void ProcessUniqueItem(RenderResult renderResult, RenderTaskUniqueItemParameters parameters)
            {
                var coverText = String.Empty;
                var coverLinks = String.Empty;
    
            var positionModelStart = renderResult.MessageBody.IndexOf("#Model.");
            if (positionModelStart > -1)
            {
                var positionModelEnd = renderResult.MessageBody.Substring(positionModelStart + 7);
    
                var modelId = positionModelEnd.Substring(0, positionModelEnd.IndexOf("#"));
    
                IPublishedContent model = helper.TypedContent(modelId);
    
                if (model != null)
                {
                    coverText = model.GetPropertyValue<string>("coverText", true);
                    var coverLinksUrls = model.GetPropertyValue<MultiUrls>("coverLinks");
    
                    if (coverLinksUrls != null && coverLinksUrls.Any())
                    {
                        coverLinks = "<td class='mcnTextContent linkBlock' style='border-left: 1px solid #c9c9c7; padding-left: 50px'>";
                        foreach (var link in coverLinksUrls)
                        {
                            coverLinks += "<p style='margin: 0'><a href='" + link.Url + "' title='" + link.Name + "' target='_blank' style='text-decoration: none; width: 100%;'>&gt; " + link.Name + "</a></p>";
                        }
                        coverLinks += "</td>";
                    }
    
                }
    
                renderResult.MessageBody = renderResult.MessageBody.Replace("#Model." + modelId + "#", String.Empty);
            }
            renderResult.MessageBody = renderResult.MessageBody.Replace("#coverText#", coverText);
            renderResult.MessageBody = renderResult.MessageBody.Replace("#coverLinks#", coverLinks);
        }
    }
    
  • Markus Johansson 1936 posts 5864 karma points MVP 2x c-trib
    Dec 11, 2015 @ 09:32
    Markus Johansson
    0

    Hi!

    Are you sure that the event is firing before you start sending?

    // m

  • Mary 15 posts 123 karma points
    Dec 11, 2015 @ 13:14
    Mary
    0

    Hi Markus

    When I start sending a Newsletter, it goes first of all to the method ProcessPreRender, then to the event SendNewsletterService.SendningInitializing += (sender, args) and then to the method ProcessUniqueItem()

    Is that incorrect? If yes, how can I change this?

    Thanks!

  • Markus Johansson 1936 posts 5864 karma points MVP 2x c-trib
    Dec 15, 2015 @ 08:34
    Markus Johansson
    0

    That sounds correct - the ProcessUniqueItem-method is called in a new thread and that's why the context is null but the code in the SendningInitializing -method should reinstantiate the UmbracoContext.

    Could we talk over PM so that we can share some code? Send me an e-mail: markus [at sign goes here] enkelmedia.se.

    Cheers!

  • Mary 15 posts 123 karma points
    Jan 08, 2016 @ 10:10
    Mary
    100

    I was able to resolve this issue with setting the HttpContext and the UmbracoContext newly in the ProcessUniqueItem method as followed:

    public override void ProcessUniqueItem(RenderResult 
    
    renderResult, RenderTaskUniqueItemParameters parameters)
    {
    
                    HttpContext.Current = this.httpContext;
    
                    UmbracoContext.EnsureContext(httpContext: this.umbracoContext.HttpContext,
                        applicationContext: ApplicationContext.Current,
                        webSecurity: new WebSecurity(this.umbracoContext.HttpContext, ApplicationContext.Current),
                        replaceContext: true);
    
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft