Sitecore workflow approval/rejection emails
Asked Answered
V

4

6

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);
}
Viceregent answered 13/6, 2011 at 13:7 Comment(1)
I've seen the following but it is of little help: trac.sitecore.net/MailWorkflowAction/browser/Trunk/sources/…Viceregent
V
8

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
    }
}
Viceregent answered 13/6, 2011 at 19:43 Comment(1)
Every command must have its own class / Process() method... you've stumbled onto the solution here already. There is no differentiation per se, Sitecore is just calling the unique method defined for each command.Rain
S
3

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.

Stibine answered 13/6, 2011 at 14:1 Comment(0)
A
0

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

Amylose answered 14/6, 2011 at 5:1 Comment(0)
R
0

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
Raki answered 13/10, 2016 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.