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.
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)
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...;-)
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:
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:
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
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
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
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
Good to know! Thanks again!
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
And the AddAlertBubble function is just has what you already have:
is working on a reply...