If you needed to tap into a list of activities of orchard, unpublished activity is one of the requirements. For your information, Orchard default Workflow activity list didn’t have a unpublished activity. Maybe you had built a module that added your own custom Workflow Activities, then you had successfully creating the activities and making them work. But there's a problem that you had no idea how to bind one of these with an event. Even if you copied the publish activity which is found in the default activity folder of Workflow module, the copied activity didn’t get bind to any event. So, in this post, I will tell you the simple way to create costum workflow activity for unpublished.

First thing, you have to find that Orchard.Workflows.Activitieshas 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 this tutorial, I created a handler that would listen to that event. Here are the codes:

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 you create your 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."); }
}
}
}

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.

 

Orchard CMS 1.8 with Free ASP.NET Hosting
Try our Orchard CMS 1.8 with Free ASP.NET Hosting today and your account will be setup soon! You can also take advantage of our Windows & ASP.NET Hosting support with Unlimited Domain, Unlimited Bandwidth, Unlimited Disk Space, etc. You will not be charged a cent for trying our service for the next 3 days. Once your trial period is complete, you decide whether you'd like to continue.