Search code examples
c#asp.netsitecoresitecore6

Sitecore workflow approval/rejection emails


We are working on implementing some custom code on a workflow in a Sitecore 6.2 site. Our workflow currently looks something like the following:

example sitecore workflow

Our goal is simple: email the submitter whether their content revision was approved or rejected in the "Awaiting Approval" step along with the comments that the reviewer made. To accomplish this we are adding an action under the "Approve" and "Reject" steps like so:

sitecore workflow with actions

We are having two big issues in trying to write this code

  1. There doesn't seem to be any easy way to determine which Command was chosen (the workaround would be to pass an argument in the action step but I'd much rather detect which was chosen)
  2. I can't seem to get the comments within this workflow state (I can get them is the next state though)

For further context, here is the code that I have so far:

var contentItem = args.DataItem;
var contentDatabase = contentItem.Database;
var contentWorkflow = contentDatabase.WorkflowProvider.GetWorkflow(contentItem);
var contentHistory = contentWorkflow.GetHistory(contentItem);

//Get the workflow history so that we can email the last person in that chain.
if (contentHistory.Length > 0)
{
    //contentWorkflow.GetCommands
    var status = contentWorkflow.GetState(contentHistory[contentHistory.Length - 1].NewState);

    //submitting user (string)
    string lastUser = contentHistory[contentHistory.Length - 1].User;

    //approve/reject comments
    var message = contentHistory[contentHistory.Length - 1].Text;

    //sitecore user (so we can get email address)
    var submittingUser = sc.Security.Accounts.User.FromName(lastUser, false);
}

Solution

  • I ended up with the following code. I still see no good way to differentiate between commands but have instead implemented two separate classes (one for approve, one for reject):

    public void Process(WorkflowPipelineArgs args)
    {
        //all variables get initialized
        string contentPath = args.DataItem.Paths.ContentPath;
        var contentItem = args.DataItem;
        var contentWorkflow = contentItem.Database.WorkflowProvider.GetWorkflow(contentItem);
        var contentHistory = contentWorkflow.GetHistory(contentItem);
        var status = "Approved";
        var subject = "Item approved in workflow: ";
        var message = "The above item was approved in workflow.";
        var comments = args.Comments;
    
        //Get the workflow history so that we can email the last person in that chain.
        if (contentHistory.Length > 0)
        {
            //submitting user (string)
            string lastUser = contentHistory[contentHistory.Length - 1].User;
            var submittingUser = Sitecore.Security.Accounts.User.FromName(lastUser, false);
    
            //send email however you like (we use postmark, for example)
            //submittingUser.Profile.Email
        }
    }