ContentHandler.OnPublished event triggered at wrong time

Topics: Troubleshooting, Writing modules
Mar 26, 2011 at 5:27 PM
Edited Mar 26, 2011 at 6:30 PM


I'm writing a module that's designed to send email notifications to configured users whenever a particular content type is published.

Here's the code for my part handler:


    public class PublishNotificationPartHandler : ContentHandler

        private readonly IContentNotificationService _notificationService;
        public PublishNotificationPartHandler(IRepository<PublishNotificationPartRecord> repository, IContentNotificationService notificationService)
            _notificationService = notificationService;


            // Hook into publishing event
            OnPublished<PublishNotificationPart>((context, part) =>

                var settings = part.Settings.GetModel<PublishNotificationTypePartSettings>();
                if (settings == null) return;

                _notificationService.SendNotification(settings, part.ContentItem);



The problem I'm seeing is that the OnPublished delegate gets called immediately after tabbing out of the title textbox (i.e. when the slug is being generated). It then gets called again when I finally click "Publish" - when it should do. Obviously the service that builds the unique route slug is for some reason triggering OnPublished, but I couldn't see anything in the event context to differentiate between that and an actual Publish.

This is happening in current dev branch, haven't tested in 1.020. Is anyone aware of this, should I file a bug?




The source appears to be in Orchard.Core.Routable.Controlls.ItemController line #86:


I also found the following code in Orchard.Code.Contents.ViewModels.PublishContentViewModel:

        public bool IsPublished {
            get { return ContentItem.VersionRecord != null && ContentItem.VersionRecord.Published; }

        public bool HasDraft {
            get {
                return (
                    (ContentItem.VersionRecord != null)
                    && ((ContentItem.VersionRecord.Published == false)
                        || (ContentItem.VersionRecord.Published && ContentItem.VersionRecord.Latest == false)));

        public bool HasPublished {
            get { return IsPublished || ContentItem.ContentManager.Get(ContentItem.Id, VersionOptions.Published) != null; }

Now I've tried all these methods of distinguishing published from draft and they all still indicate published, even ContentItem.ContentManager.Get(ContentItem.Id, VersionOptions.Published) which returns an item even though the item has clearly not yet been published!

I'm not sure how to work around this, or why the OnPublished event doesn't get called on the Save button (which could clue me in to a solution).

If anyone can offer any suggestions I'd be most grateful!

Update 2:

I commented out the Orchard.Core.Routable.Controlls.ItemController line #86. It's just the Slugify method and shouldn't be publishing anything. Slugify still seems to work. Am I missing something or should that line just not be in there?

Mar 26, 2011 at 8:57 PM

I think you are right, please file a bug.

Mar 26, 2011 at 10:49 PM
Edited Mar 26, 2011 at 10:50 PM

Raised work item:

With patch attached, hopefully it's that simple and the call just shouldn't have been there :)

Mar 27, 2011 at 3:22 AM


Jul 21, 2011 at 7:42 AM

i want to do something like this too. but i don't know where to start.

your guy can give me some information to start it?



Jul 21, 2011 at 11:21 PM

Do something like what? Can you specify exactly what you are trying to do?

Jul 22, 2011 at 3:36 AM

thanks, i can do it now. i just want to send email notification when published.