Tap into Unpublished Activity in workflow module

Topics: Customizing Orchard, General, Writing modules
Mar 3, 2014 at 8:38 AM
Edited Mar 7, 2014 at 6:06 AM
I need to tap into a list of activities of orchard, Unpublished activity being one of the requirements. I have looked and found out that Orchard default Workflow activity list doesn't have a unpublished activity.

I have built a module that add my own custom Workflow Activities. I have had no problem creating activities and making them work but I have no idea how to bind one of these with an event. Even if I copy the publish activity that is found in the default activity folder of Workflow module, the copied activity doesn't get bind to any event.

How can I make it so that my activity class is called whenever someone publishes or drafts a post.
Mar 7, 2014 at 6:08 AM
I couldn't find much help on Orchard CMS and ended up finding a solution myself. It took me a lot of time to get this done though.

First thing I found was that Orchard.Workflows.Activities has a file ContentActivity. In this file there are other classes that inherits the ContentActivity class ContentCreatedActivity, ContentUpdatedActivity and ContentPublishedActivity. All these classes are activities that subscribe to ContentActivity that is an event activity. They subscribe to the Create, Update and Publish events of the Orchard core.

If you look into Orchard.ContentManagement.Handlers.ContentHandler you'd see the list of default events provided by Orchard CMS core.

I was interested in the OnUnpublished event, so in my module I created a handler that would listen to that event.

    using System.Collections.Generic;
    using Orchard.ContentManagement;
    using Orchard.ContentManagement.Handlers;
    using Orchard.Workflows.Services;

    namespace MyModule.Handlers {
        public class WorkflowContentHandler : ContentHandler {
            public WorkflowContentHandler(IWorkflowManager workflowManager) {
                OnUnpublished<ContentPart>(
                    (context, part) =>
                        workflowManager.TriggerEvent("ContentUnpublished",
                        context.ContentItem,
                        () => new Dictionary<string, object> { { 
                                  "Content", context.ContentItem } }));
            }
        }
    }
After which we create our custom workflow activity for Unpublished. This class inherits from ContentActivity like its siblings, so it can start workflow and would be an event.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Orchard.Localization;
    using Orchard.Workflows.Models;
    using Orchard.Workflows.Services;
    using Orchard.Workflows.Activities;

    namespace MyModule.WorkFlow
    {
        public class ContentUnpublishedActivity : ContentActivity
        {
            public override string Name
            {
                get { return "ContentUnpublished"; }
            }

            public override LocalizedString Description
            {
                get { return T("Content is Unpublished."); }
            }
        }
    }
And that's it. Once you've done this the new Content Unpublished activity would show up in the Workflow activity list. You can use it in conjunction to other Activities to execute your own workflow after any content has been unpublished.

Content Unpublished Activity in Workflow list

I can't believe it was this easy. Took me 3 days to figure it out and I was pulling my hair that I don't have much of to that start with. The lack of support and resources for Orchard CMS really annoys me sometime. I hope this would help save some time for anyone who has run into similar problems.
Marked as answer by sworup on 3/6/2014 at 10:08 PM