sestocker
sestocker

Reputation: 3532

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);
}

Upvotes: 6

Views: 3693

Answers (4)

Girija Belavadi
Girija Belavadi

Reputation: 1

This will give you the GUID for commands like submit, reject etc.

args.CommandItem.ID 

This will give you the GUID for states like Draft, approved etc.

args.CommandItem.ParentID

Upvotes: 0

marto
marto

Reputation: 4180

I have answered a very similar question.

Basically you need to get the Mail Workflow Action and then you need to further extend it to use the original's submitter's email.

Upvotes: 3

dmgdotnet
dmgdotnet

Reputation: 362

Easiest way to get the command item itself is ProcessorItem.InnerItem.Parent

Upvotes: 0

sestocker
sestocker

Reputation: 3532

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

Upvotes: 8

Related Questions