Sunday, March 31, 2013

SDL Tridion 2011 SP1 - Bypass first workflow activity

Back in my consulting days, when delivering training on workflow, I would start by explaining what workflow is and how in Tridion, workflow's meaning is not the same as what a business would understand its own business workflow to be. Then I would explain what this difference was and proceed along to describe what each little workflow building block is used for.

In SDL Tridion 2011 (and 2009), all items starting workflow automatically enter a manual activity assigned to everyone with the intent of handling or better yet locking any editing or update henceforth. *

As useful and purposeful as this action was, inevitably, I would always get the following question: "This is great! Now, how can we bypass it?"

Seeing as there are indeed reasons to bypass the first activity, for example if there is a need to introduce a decision activity for logic to direct flow of user actions differently based on some criteria, below is some code that will accomplish this from an Event System.

public class TestFastForward : TcmExtension {
public TestFastForward() {
//EventSystem.Subscribe(OnComponentSavePost, EventPhases.TransactionCommitted); EventSystem.Subscribe< Component , CheckInEventArgs >(OnComponentCheckIn, EventPhases .TransactionCommitted);        
}        
//private void OnComponentSavePost(Component comp, SaveEventArgs args, EventPhases phases) private void OnComponentCheckIn( Component comp, CheckInEventArgs args, EventPhases phases) {            
try {                
XmlElement xmlElement = comp.ToXml();                
XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager (new NameTable());                
xmlNamespaceManager.AddNamespace( "tcm" , "http://www.tridion.com/ContentManager/5.0" ); String activityInstanceId = xmlElement.SelectSingleNode("//*[local-name()='ActivityInstance']" , xmlNamespaceManager).Attributes[ "xlink:href" ].Value;                
ActivityInstance activityInstance = new ActivityInstance( new TcmUri (activityInstanceId), comp.Session);                
ActivityFinish activityFinishData = new ActivityFinish ( "Automatically finished activity " + activityInstance.Title, null , comp.Session);                
ActivityInstance nextActivity = activityInstance.Finish(activityFinishData);            
}            
catch (Exception ex) { throw new Exception(ex.Message);            
}        
}

In the code above, I retrieve the ActivityInstance from the current component in the active session. Then I use information from it to advance to the next activity, whatever this may be as designed in the Visio diagram. Even though an item is in workflow the CheckIn event is still called when an Editor clicks Save & Close, the same code will work in the post Save event for an item.

* In SDL Tridion 2013, items do not automatically enter workflow as soon as an item is created or edited, but rather when an Editor clicks Enter Workflow.