Copied to clipboard

Flag this post as spam?

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


  • higgsy 65 posts 92 karma points
    Aug 21, 2012 @ 17:11
    higgsy
    0

    Umbraco events i.e. BeforePublish not firing

    Hi all,

    I am trying to hook into the BeforePublishing event to apply some custom validation for a particular document type. Based on the article at http://blog.mattbrailsford.com/2010/07/11/registering-an-application-start-event-handler-in-umbraco/ I created a class which inherits from umbraco.Global, however none of the events Application_Start etc appear to fire (confirmed by debugging). I am using version 4.7.1 and my code is below:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    /// <summary>
    /// Summary description for Global
    /// </summary>
    public class Global : umbraco.Global
    {
        bool _beforePublishEventAttached;
        protected override void Application_Start(object sender, EventArgs e)
        {
            base.Application_Start(sender, e);
            if (!_beforePublishEventAttached)
            {
                umbraco.cms.businesslogic.web.Document.BeforePublish += new umbraco.cms.businesslogic.web.Document.PublishEventHandler(Document_BeforePublish);
            }
        }
        protected override void Application_End(object sender, EventArgs e)
        {
            base.Application_End(sender, e);
            if (_beforePublishEventAttached)
            {
                umbraco.cms.businesslogic.web.Document.BeforePublish -= new umbraco.cms.businesslogic.web.Document.PublishEventHandler(Document_BeforePublish);
                _beforePublishEventAttached = false;
            }
        }
        void Document_BeforePublish(umbraco.cms.businesslogic.web.Document sender, umbraco.cms.businesslogic.PublishEventArgs e)
        {
            if (sender.ContentType.Alias == "Service")
            {
                if (sender.getProperty("showWidget").Value == "1" && (string.IsNullOrEmpty(sender.getProperty("widgetTitle").Value.ToString())))
                {
                    e.Cancel = true;
                }
            }
            _beforePublishEventAttached = true;
        }
    }

    I have two questions. 1) Am I doing anything wrong? The article also mentions creating a Global.asax file and inheriting, but that feels a bit like a hack. 2) Is there anyway of returning a message to the editor when the Publish event is cancelled - it's going to be a bit confusing to them if they don't know the reason why the action was cancelled.

    Thanks in advance

    Al

  • Lennart Stoop 304 posts 842 karma points
    Aug 21, 2012 @ 17:26
    Lennart Stoop
    0

    Hi Al,

    I would recommend using ApplicationBase to hook into the document publishing events, much like shown in the example here:

    http://our.umbraco.org/wiki/reference/api-cheatsheet/using-applicationbase-to-register-events

    Having the speech bubble updated with a custom message is not possible in 4.7.1 but it's a known issue (I'm not sure if it has been resolved in 4.8 or 4.8.1)

     

    Grtz

    L

     

  • higgsy 65 posts 92 karma points
    Aug 21, 2012 @ 18:08
    higgsy
    0

    Hi Lennart,

    Thanks for the guidance - it's odd that some peopel have managed to make the class approach work when inheriting from umbraco.Global - oh well at least it is working!

    At least the speech bubble issue is recognised, hopefull just adding the ability to set e.CancelReason shouldn't be too much work for the team in the future....same approach as EPiServer then...;-)

    Thanks again

    Al

  • Lennart Stoop 304 posts 842 karma points
    Aug 21, 2012 @ 19:32
    Lennart Stoop
    0

    I would think the approach using umbraco.Global comes from earlier versions of Umbraco. I have used it myself a couple of times to configure log4net or IoC in application start/end (having to remove the global.asax.dll from the bin folder) but I have to agree it doesn't feel right and I wouldn't want all my custom event handling in there either :-)

    There seems to be a fix for the speech bubble in 4.8 by the way:

    http://issues.umbraco.org/issue/U4-84#tab=History

     

    Grtz

    L

     

  • higgsy 65 posts 92 karma points
    Aug 21, 2012 @ 21:12
    higgsy
    0

    Good to know! Thanks again!

  • Murray Roke 503 posts 966 karma points c-trib
    May 28, 2014 @ 05:02
    Murray Roke
    0

    Speech bubble seems still broken in 4.11.x

    You can work round this by injecting your speech bubble later in the page life cycle like so

    var page = HttpContext.Current.Handler as Page;
    if (page != null)
    {
        page.PreRenderComplete += AddAlertBubble;
    }
    

    And the AddAlertBubble function is just has what you already have:

    void AddAlertBox(object sender, EventArgs e)
    {
        umbraco.BasePages.BasePage.Current.ClientTools.ShowSpeechBubble(umbraco.BasePages.BasePage.speechBubbleIcon.error, "Error", "Problem");
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft