I came across a requirement recently that needed a page to be published automatically when any component on it completed workflow. Here are some pointers if you need to do this as well. Â Note, this tip is based on Tridion 2009, however, the lessons provided here may also apply to Tridion 2011.
Publishing From an Automatic Workflow Activity Won’t Help
Sending your page to the publishing queue from within an Automatic Activity won’t do any good since your item is still in workflow, and only the major version (i.e. the last approved version) gets published. Â This makes perfect sense. Â You don’t want to publish to your Live site items that are still in draft status. Â
Although there is a way to publish using the latest version of the item. Â You can set the activateWorkflow flag = true to take the latest minor version (including the one inside the workflow). Â However, if you have other components in a page also going through workflow in their early draft states, you definitely don’t want them considered. So the activateWorkflow flag won’t be of help here.
- Use the activateWorkflow=true to publish to Staging/Preview environment. Don’t use it for publishing to Live.
Publishing From the Event System
- OnProcessInstanceFinishPost
- OnActivityInstanceFinishPost
OnProcessInstanceFinishPost
The good thing is that it’s not the end of the world. Â So we look upon the next available event.
OnActivityInstanceFinishPost
This event fires upon completion of each activity. Â So we simply check that the activity instance this is being triggered for is the last activity in the flow. Â So far so good…
The input parameter into this event is the ActivityInstance that just finished. Â So now all we need to do is get the workflow item, i.e. the component that just finished being edited, and send it off to the publishing queue (assuming you have the default cascading publishing enabled that will trigger the publishing of the page(s) on which the component is included, and the actual page has been previously published [because the cascade publishing of a component only publishes a page if the page has been explicitly published]). Â Well, you can hold your horses right there…
The way you would normally get the WorkItem object from the ActivityInstance is something like this:
<br /> Component comp = (Component)activityInstance.ProcessInstance.Item;<br />
This is works great for all activities, except… wait for it… the last one. Â Upon completing the last activity the process instance is done, meaning that activityInstance.ProcessInstance.Item will throw a big fat Null Reference exception.
So how do you get the WorkItem of a process instance that no longer exists? Â Here is how…
Turns out that the ProcessInstance in this case isn’t null, and one of the few properties we still have access to is its TCM ID. Â However, other property that gets lazy loaded isn’t available. Â Another interesting thing to know is that when a ProcessInstance completes it becomes a ProcessHistory item.
And the TCM ID of a ProcessInstance is the same as the TCM ID the ProcessHistory, with the exception of the type. Â Here is what I mean:
ProcessInstance TCM ID: Â tcm:XX-YY-131076
ProcessHistory TCM ID: Â Â tcm:XX-YY-131080
The only difference is the type, 131076 for ProcessInstance and 131080 for ProcessHistory.
So in our OnActivityInstanceFinishPost event, we derive the ProcessHistory based on the ProcessInstance ID and then get our component workitem.
string processHistoryId = activityInstance.ProcessInstance.ID.Replace("131076", "131080"); ProcessHistory hist = (ProcessHistory)tdse.GetObject(processHistoryId, EnumOpenMode.OpenModeView); Component comp = hist.Item as Component;So now we can happily trigger publishing of the component: comp.Publish(…)