Monday, April 15, 2013

Assign Back to Author ( Who last worked on Content Edition on that component) - Workflow

Often, I get below requirement in workflow

On Reject --> Workflow should be assigned back to author.
However, what happens if Business admin does "Assign Activity" if  content is assigned back to a different author at any point of workflow (Since initial author was on vacation or may be even he/she left company) In that case content should be picked up by other author.

In this case a new author is assigned to the content. And On Reject content should be going back to original author.

On Assign Back to Author content should go back to Activity Name: Content Creation
and ASSIGNED TO LAST PERFORMER OF THIS ACTIVITY



SCRIPT CODE of  Assign Back to Author

                                                      C# Code


public void BackToAuthor(string workitemid)
{
try
{
Stopwatch _watch = new Stopwatch();
_watch.Start();
Logger.Debug("Entered RPPTridionWorkflow BackToAuthor");
WorkItemData workitem = (WorkItemData)CoreServiceClient.Read(workitemid, options);
ProcessInstanceData processInstance = (ProcessInstanceData)CoreServiceClient.Read(
  workitem.Process.IdRef, options);
ActivityData[] ieActivities = processInstance.Activities;

if (ieActivities != null)
{
int currentActivity = ieActivities.Length - 1;
var targetactivity = (ActivityInstanceData)CoreServiceClient.Read(
 processInstance.Activities[currentActivity].Id, options);
ActivityData creatorActivityData = getActivityDataBasedOnName(workitemid, "Content Creation");
if (creatorActivityData != null)
{
var finishData = new ActivityFinishData();

finishData.Message = "Item Rejected";
finishData.NextAssignee = new LinkToTrusteeData
{
//This is where I retrieve the ACTIVITY INFORMATION of activity " Content Creation" using method getActivityDataBasedOnName(workitemid, "Content Creation"); 
IdRef = creatorActivityData.Owner.IdRef,
Title = creatorActivityData.Owner.Title
};
CoreServiceClient.FinishActivity(targetactivity.Id, finishData, options);
}
else
Logger.Error("Content Creation Activity not Found in BacktoAuthor");

Logger.Debug("Closing Core Service session:  BackToAuthor [" + _watch.ElapsedMilliseconds + " ms]");
}
}
catch (Exception ex)
{
Logger.Error("Exception in BackToAuthor" + ex.Message);

}
finally
{
CoreServiceClient.Close();
}
}
<!------------------ This method reads list of activities performed during the workflow. and get the activity information based on name. It always gets information of the activity the last time this is performed  -------------->

public ActivityData getActivityDataBasedOnName(string workitemid, string activityName)
{
WorkItemData workitem = (WorkItemData)CoreServiceClient.Read(workitemid, options);
ProcessInstanceData processInstance = (ProcessInstanceData)CoreServiceClient.Read(
  workitem.Process.IdRef, options);
ActivityData[] ieActivities = processInstance.Activities;
ActivityData lastActivityData = null;
foreach (ActivityData actData in ieActivities)
{
if (actData.Title.Contains(activityName))
lastActivityData = actData;
}
return lastActivityData;
}

1 comment:

  1. Why do you call getActivityDataBasedOnName, which read the WorkItem and full ProcessInstance again, when you already have the full ProcessInstace?
    Wouldn't this do the same:
    ActivityData creatorActivityData = ieActivities.LastOrDefault(ad => ad.Title.Contains("Content Creation"));

    ReplyDelete