My workflow for comment notifications

Workflows in Orchard 1.7 are a damn sweet feature, and in this post I’m going to show you a very simple and useful case: comment moderation and notifications.

Let’s begin by going to the Modules screen and checking that the older Rules module is disabled, and the Workflows is enabled. Once this is done, let’s click on Workflows in the admin menu and click on “Create a new Workflow Definition” on the top-right of the screen.

You’ll be prompted for a name. Let’s choose “Comment notification” and hit Save. You should now have a blank design surface for the workflow. Let’s drag the Content Published activity onto the design surface:Dragging the content published activity onto the design surface

Then click on the activity and select the pencil icon to switch to its edit screen:Edit the activity

Find the Comment content type, select it, and save. This sets up the first step of our workflow, to trigger when a comment publication is attempted by a user. If you have comment moderation on, this means the the user submitted the comment, not that it was actually validated and published onto the public web site. If you have spam protection on, this won’t get triggered for failed captchas.

Drag a new Send Email activity onto the surface, next to the first activity. Then grab the blue ball on top of the Content Published activity and drag it onto the new email one:Connect the output of the Content Published activity to the Send Email activity

Click the email activity and select the edit button.Edit the email activity

Fill the edit form as follows:The Email activity configuration

The pattern for the subject is:

A new comment was published on {Site.SiteName}

And the pattern for the body is:

<h3>A new comment was published on {Site.SiteName} by {Content.CommentAuthor}</h3>

<p>{Content.CommentMessage}</p>

<div>
  <a href="{Site.BaseUrl}/Admin/Comments">Moderate comments</a> |
  <a href="{Content.CommentApproveUrl}">Approve</a> |
  <a href="{Content.CommentModerateUrl}">Disapprove</a> |
  <a href="{Content.CommentDeleteUrl}">Delete</a>
</div>

Here we are using a bunch of tokens, which are globally accessible and contextual variables that may be used in Orchard in various places to configure features such as emails dynamically, from the admin UI. All the expressions that are between curly braces will be replaced in the e-mail with actual values.

In this template, we have a notification that a comment was published, then we have the actual text of the comment, and then we have four buttons that will enable the comment moderator to act on that comment in one click, directly from his e-mail client.

The last thing we have to do (and this one is easy to forget, believe me), is to tell the workflow engine what activity should start the workflow. In our case, that should be the Content Published activity, so let’s select the activity and click its first button:Set the Published activity as the start of the workflow.

The outline and background of the activity should have changed to reflect its new status.

We can now save the workflow and test it.

Now every time someone submits a comment, the site administrator will receive an e-mail looking like this:image

Note that for this to work, you need to have properly configured e-mail settings for Orchard. Doing so is out of the scope of this post.

With this, you can always keep an eye on your user’s activity on your site. This example also demonstrates some usage of workflows and of tokens, two must-know features of Orchard. I hope this helps.

UPDATE: it is a good move to add a delay activity between the update activity and the send mail activity, even if it's short, so that the e-mail sending doesn't happen on the main thread and doesn't block. Thanks to Zoltan for the tip.

8 Comments

  • It's also a good thing to add a timer between your published event and the e-mail task. This way e-mail sending won't be blocking (which is only an issue of course if it takes a noticeable) but instead be handled in the background later. With the latest source URL tokens also work in background tasks, so you can have links in your notifications to the commented item and it will still work.

  • I love the Workflow stuff in Orchard. It is truly amazing work, and so very powerful.

    If I could improve anything on it, it would be to ditch the monochrome nature of it, and give it some life. Categorize the types of actions, and color code them to help visualize a flow.

    The "power" button for the action needs to be instantly recognizable whether the action is turned on or off. Right now, you go from white to a very light gray, with no legend to indicate which is "on" and which is "off". Actions that are turned on should be tinted green, and actions that are turned off should be tinted red.

    If you can fix that, this module would be perfectly executed.

  • It would be nice if you can also send those notifications to the author of the article. For example, on our website we use a blog for news. There are multiple people who add news articles. It would be usefull if those people can receive a notification when someone comments on their news article.

  • @Walance: take a look at the email settings screenshot a little more closely. You can set an arbitrary sendee, and this can be token-based, so you should be able to use {Content.CommentedOn.Author.Email}

  • Ah yes... for some reason I never noticed that you can use tokens in that field.. I'm sorry for asking, I should have seen that.
    Thanks for your answer :)

  • Never be sorry for asking ;) It's a great question.

  • Is it possible to trigger off the creation of a part record?

    I have a scenario where I want to trigger the start of a work flow when an order gets created, but the order has no content type associated to it. If that's not possible, could I trigger the start of a workflow manually after creating the order record in my service class?

    Is this where the Signal activity comes in? If so, how do I set that up in the workflow and call into a signal?

  • @Marcus: Not with the built-in activities, but it should be easy to build a new one that does that. You could also use the signal activity, yes. Import ISignal in your service and use it to emit a signal, that your activity can then pick-up.

Comments have been disabled for this content.