Workflow handler

  Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic! Mail us feedback on this topic!  

The workflow handler allows you to handle the following events:

 

OnBeforeCheckOut - before document is checked out
OnAfterCheckOut - after document is checked out
OnBeforeCheckIn - before document is checked in
OnAfterCheckIn - after document is checked in
OnBeforeApprove - before document is approved
OnAfterApprove - after document is approved
OnBeforeReject - before document is rejected
OnAfterReject - after document is rejected
OnBeforePublish - before document is published
OnAfterPublish - after document is published

 

When the document is published, the order of the events is following:

 

1.OnBeforeApprove
2.OnBeforePublish
3.OnAfterPublish
4.OnAfterApprove

 

Example

 

The following example shows how to send an e-mail with news document content when it's published:

 

1.Open the CustomWorkflowHandler class and put the following code at the beginning of the file. It adds the reference to the namespaces we will use to handle the event:

 
[C#]

 

using CMS.TreeEngine;

using CMS.GlobalHelper;

using CMS.EmailEngine;

using CMS.WorkflowEngine;

 

 

2.Put the following code inside the OnAfterPublish method:

 

[C#]

 

// type the document as TreeNode

TreeNode newsDoc = (TreeNode)treeNodeObj;

 

// handle the event only for news items

if (newsDoc.NodeClassName.ToLower() == "cms.news")

{

  // get content of the inserted news item and send it by e-mail

  EmailMessage email = new EmailMessage();

  email.From = "admin@domain.com";

  email.Recipients = "admin@domain.com";

  email.Subject = ValidationHelper.GetString(newsDoc.GetValue("NewsTitle"), "");

  email.Body = ValidationHelper.GetString(newsDoc.GetValue("NewsSummary"), "");

  EmailSender.SendEmail(email);

}

 

 

3.Set the From and Recipients e-mail addresses to you e-mail address.
 
4.Compile and run the project.
 
5.Configure your project so that it uses workflow for news items and create and publish a news item. You should receive the content of the news item by e-mail.

 

 

Getting the workflow step name

 

If you need to get the name of the workflow step (for example in the OnAfterApprove event), you need to use code like this:

 

[C#]

 

CMS.WorkflowEngine.WorkflowStepInfo previousStep = (CMS.WorkflowEngine.WorkflowStepInfo) previousStepObj;

string stepCodeName = previousStep.StepName;

 

Page url: http://devnet.kentico.com/docs/devguide/index.html?workflow_handler.htm