How to get notified whenever an item containing a particular part gets published

Topics: Writing modules
Jan 11, 2013 at 10:26 PM

I need to know whenever a item containing a part get published.


When any item that contains the part gets published I would like to go ahead and call a service passing along all of the items information.


Does that make sense? Anyone know if there is an event that I can hookup to or something like that?


Thanks,

Developer
Jan 11, 2013 at 10:35 PM

I don't know of any such event, so you may have to write your own.

Jan 11, 2013 at 11:21 PM

I think the OnPublishing would probably work. However, I didn't see something there that would allow me to fail the publication, for instance.

As I would like to get all the information from the item, then submit it to another service for additional validation.

Developer
Jan 11, 2013 at 11:27 PM
Edited Jan 11, 2013 at 11:27 PM

Sure, but you mentioned when "the part" gets published, which suggests to me not just a content item with any part but a specific part. I don't think there's a configuration option for that out of the box.

Jan 11, 2013 at 11:29 PM

Sorry, I meant a content item that a particular part, a part that I created. In that case OnPublishing would solve the problem for me, I just don't know how to stop the publishing if my call to my service fails or I get some error indicator from it.

Developer
Jan 11, 2013 at 11:52 PM

Perhaps you mean a Content Type that you created? Because you do get to specify that.

Out of the box, there is the OnPublished event (I did not see an OnPublishing event. If you do, then please ignore the rest of this post :).

Handling the OnPublished event would be too late for you to cancel publication, so you would probably have to implement your own event that is triggered during the OnPublishing event ("contenthandler" event, not "rules" event). The OnPublishing event receives a context that has a Cancel property that you can set to cancel the item from being published.

However, even when you do implement your own "OnPublishing" event and trigger that from your content handler, I don't think there's currently a way to inspect results from actions in order to set the Cancel property of the PublishContentContext.

Now I happen to know that Sebastien is working on some very cool workflow implementation that may replace the rules engine, which may support this kind of scenario without writing code, I don't know.

However, until then, you could implement a content handler in code that simply cancels the publication of the content item if your service call fails by handling the OnPublishing event.

Jan 12, 2013 at 1:05 AM

This is kind of what I am looking for:

public class CustomPartHandler : ContentHandler
    {
        private readonly INotifier _notifier;
        private readonly Localizer _T;

        public ProductPartHandler(IRepository<CustomPartRecord> repository, INotifier notifier)
        {
            Filters.Add(StorageFilter.For(repository));

            _notifier = notifier;
            _T = NullLocalizer.Instance;

            OnPublishing<CustomPart>((context, part) =>
            {
                // call service

                if (callFailed)
                {
                    _notifier.Error(_T("Publication failed!"));
                    context.Cancel();
                }
            });
        }
    }

This context does not have a cancel and I couldn't really find a cancel in the source code.

And let me try to explain again what I am trying to do: I want to create a part that can be attached to any type. Now, the user can create a type, attach the part and when the user tries to publish an item that contains that part I want to run some custom validation and then depending on what happens there cancel the whole publication.

Developer
Jan 12, 2013 at 9:22 AM

I see. It's a new property and is available when you're cloning the 1.x branch, so it is not part of 1.6 but will be part of 1.7: http://orchard.codeplex.com/SourceControl/changeset/view/3b2d1e68942d#src/Orchard/ContentManagement/Handlers/PublishContentContext.cs