I fixed the following error in a Bundle Workflow process. I couldn't find this specific scenario online (except for a somewhat related SO question); see answer below.
System.ArgumentException: An item with the same key has already been added. at Tridion.ContentManager.Workflow.WorkflowAgentEngine.ProcessActivity(TcmUri activityInstanceId)
This example is adapted from Build Block's 2013 preview example with a minor update for to CurrentActivityInstance
, which Eric Huiza describes as one of the variables we get "for free" with Automatic Activities (similar to C# fragments, but for Core Service with Workflow).
<%@ Import Namespace="System.ServiceModel"%>
<%@ Import Namespace="System.Runtime.Serialization"%>
// Adapted from Post-Bootcamp example from Building Blocks
//
PublishInstructionData publishInstruction = new PublishInstructionData();
publishInstruction.ResolveInstruction = new ResolveInstructionData();
publishInstruction.RenderInstruction = new RenderInstructionData();
publishInstruction.ResolveInstruction.IncludeWorkflow = true;
ActivityInstanceData activityInstance = CurrentActivityInstance;
IList<String> itemsToPublishList = new List<String>();
//Staging publication target URI
String[] targets = new[] { "tcm:0-1-65537" };
foreach (WorkItemData wid in activityInstance.WorkItems)
{
int value = Convert.ToInt32(Enum.Parse(typeof(ItemType), "VirtualFolder"));
if (wid.Subject.IdRef.EndsWith(value.ToString()))
{
itemsToPublishList.Add(wid.Subject.IdRef);
}
}
PublishTransactionData[] publishTransactions = SessionAwareCoreServiceClient.Publish(itemsToPublishList.ToArray<String>(), publishInstruction, targets, PublishPriority.Normal, null);
ProcessInstance.Variables.Add("PublishTransaction", publishTransactions[0].Id);
SessionAwareCoreServiceClient.FinishActivity(activityInstance.Id, new ActivityFinishData { Message = "Publish to Staging Queued: Finished Activity" }, null);
This works as a script for a single Automatic Activity, but throws an error when attempting to use the same code to publish to Staging and then Live in another activity (even when changing the Publication Target ID).