5

We have items which are associated with worfklow. This workflow has different states like draft, reviewed, approved. Whenever it is moved to next state, we enter the comments. We need to get all these comments whenever item is published and then we have to send it through email.

Is there some way to get all the workflow comments for item?

2 Answers 2

10

Using the code below, you can get comments from all workflow states of any particular item version:

Sitecore.Workflows.WorkflowEvent[] workflowHistory =
  Database.WorkflowProvider
    .GetWorkflow(item)
    .GetHistory(item);

string workflowComments = "";

foreach (Sitecore.Workflows.WorkflowEvent work in workflowHistory)
{
   workflowComments += string.Join(",", work.CommentFields["Comments"]) + " ";
}

Note that I am not using work.Text, as that field is obsolete. work.CommentFields should be used instead. This new property has added so that you can extend it to add additional fields for custom comment data.

1
  • 1
    Rohit, I have edited your answer to be more readable, and also to fix a defect in the code (you reassigned workflowComments instead of appending comments to it). I hope you don't mind this. Feel free to edit it further if you'd like. Commented Dec 14, 2016 at 8:41
8

You can always use Sitecore PowerShell Extensions if you would like to generate reports or build a UI around the history data.

$item = Get-Item -Path "master:" -Id "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}"

$db = Get-Database -Name "master"
$workflowProvider = $db.WorkflowProvider

foreach($version in $item.Versions.GetVersions()) {
    $workflowEvents = $workflowProvider.HistoryStore.GetHistory($version)
    foreach($workflowEvent in $workflowEvents) {
        "[$($workflowEvent.Date)] ($($workflowEvent.User)) $(($workflowEvent.Text -replace '(\r|\n)',''))"
    }
}

[12/14/2016 04:27:55] (sitecore\editor-eddie) Here are some comments.

[12/14/2016 04:39:55] (sitecore\approver-amie) Please try again!

[12/14/2016 04:40:13] (sitecore\editor-eddie) How about this?I tried hard.


With more recent versions of SPE (5.0) you should see a report for "Recent workflow history".

Report

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.