4
votes

Is it possible to programmatically set the workflow comment? e.g. Admin user "Reject" the item from workbox, we ask for a comment. Later on, our agent process retrives this comment and send email. Now I need my custom module to do the same thing (Admin reject functionality).

It would be the same code used by Sitecore to set the workflow comment I guess...

1

1 Answers

3
votes

Below is the code for executing any of the workflow commands assuming that you know the ID of the command item:

public bool Execute(Item item, ID commandId, string comment)
{
    var workflowId = item[FieldIDs.Workflow];

    if (String.IsNullOrEmpty(workflowId))
    {
        throw new WorkflowException("Item is not in a workflow");
    }

    IWorkflow workflow = item.Database.WorkflowProvider.GetWorkflow(workflowId);

    var workflowResult = workflow.Execute(commandId.ToString(), item, comment, false, new object[0]);
    if (!workflowResult.Succeeded)
    {
        var message = workflowResult.Message;
        if (String.IsNullOrEmpty(message))
        {
            message = "IWorkflow.Execute() failed for unknown reason.";
        }
        throw new Exception(message);
    }
    return true;
}