Orchard 1.7 - Create custom Workflow Activity for Unpublished

青春壹個敷衍的年華 提交于 2019-12-04 13:18:43

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.

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 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!