Working With Tridion Workflow Automatic Activities and VBScript

My preference for working with Tridion Workflows is to use the Event System and the Tridion API (TOM API with 5.3 and 2009, and TOM.NET API with Tridion 2011).  This is probably due to my favoritism to .NET over using VBScript (who wouldn’t favor that?).  But knowing how to use the TOM API in the VBScript sections of Automatic Activities does come in handy, and just in case anyone is working with these Automatic Activities, I thought I’d put together some quick samples of how to do some basic things. When working with the VBScrit from “Edit Script…” button, remember to reference the TOM API documentation (not the TOM.NET API docs!).

Getting the Component or Page of the workflow work item.

Dim obtItem
Set objItem = CurrentWorkItem.GetItem()

Remember that VBScript works dynamically here, and the variable Item above will be a Page or a Component based on which WorkItem it is.

Dim strMetaInfo, strPageUrl
If Not objItem.MetadataSchema Is Nothing Then
    strMetaInfo = "Metadata Schema: " + objItem.MetadataSchema.Title
    If Not objItem.MetadataFields.Item("Keywords") Is Nothing Then
        strMetaInfo = strMetaInfo + "Keywords: " + objItem.MetadataFields.Item("Keywords").Value(1)
    End If
End If
strPageUrl =  objItem.Info.PublishLocationUrl

When working with Components, you can access the component fields in a similar way to metadata fields.

strSomeField = objItem.Fields.Item("SomeField").Value(1)

When working with Workflows, you’ll probably want to deal with the various workflow objects like ProcessInstance, ProcessDefinition, ActivityInstance and ActivityDefinition.

' Get the current Process Instance object
Dim objProcessInstance
Set objProcessInstance = CurrentWorkItem.ActivityInstance.ProcessInstance

' Get all Activity Instances that has happened thus far in this Process Instance
Dim objActivityInstances
Set objActivityInstances = objProcessInstance.ActivityInstances

' Get the previous Activity Instance (usually the manual activity that led to this automatic activity)
Dim objLastActivityInstance
Set objLastActivityInstance = objActivityInstances(objActivityInstances.Count - 1)

' Get the Finish Message that was input from the previous Activity Instance
Dim strFinishMessage
strFinishMessage = objLastActivityInstance.FinishMessage

' Get the performer who finished the last activity instance
Dim objLastPerformer, strLastPerformerName
Set objLastPerformer = objLastActivityInstance.Performer
' User object's use "Name" instead of "Title" (and same with groups)
strLastPerformerName = objLastPerformer.Name

' Get the first Activity Instance
Dim objOrigActivityInstance
Set objOrigActivityInstance = objActivityInstances(1)

' Get a specific ActivityDefinition in the Process matching a specific title
Dim objAct, objActivityDefinition
For Each objAct In objProcessInstance.ProcessDefinition.ActivityDefinitions
    If objAct.Title = "Some Specific Title" Then
        Set objActivityDefinition = objAct
    End If
Next

' Get a comma separated list of user IDs from an assigned group in an Activity Definition.
Dim objTrustee, objMembersXml, strMembers
strMembers = ""
Set objMembersXml = CreateObject("MSXML2.DOMDocument.4.0")
Call objMembersXml.LoadXml(objActivityDefinition.Assignee.GetMembersList)
For Each objTrustee In objMembersXml.documentElement.childNodes
    If Len(strMembers) > 0 Then
        strMembers = strMembers + ","
    End if
    strMembers = strMembers & objTrustee.getAttribute("xlink:title")
Next

Finally another important topic of working with Workflows… automatic publishing!

Call objItem.Publish("tcm:0-1-65538", True, True, True)

You’ll want to pay attention to that third argument, especially when you want to publish a work item that hasn’t completed a workflow process yet. You’ll notice that typically, only the last COMPLETED version gets published when you put something in the queue. Setting this third argument to True ensures that the version in the work list gets published. The full method definition for Publish is as follows:

Public Function Publish( ByVal targets As Variant, ByVal activateBlueprinting As Boolean, ByVal activateWorkflow As Boolean, ByVal rollbackOnFailure As Boolean, Optional ByVal publishTime As Date = 0, Optional ByVal unpublishTime As Date = 0, Optional ByVal deployTime As Date = 0, Optional ByVal resolveComponentLinks As Boolean = True, Optional ByVal priority As TDSDefines.EnumPublishPriority = Normal, Optional ByVal ignoreRenderFailures As Boolean = False, Optional ByVal maximumRenderFailures As Long = 0 ) As String

The arguments are as follows:

targets – Specifies to/from which target(s) to (un-/re-)publish. Can be one of the following:
A TargetType object or URI
An array of TargetType URIs
A TargetTypes collection object
A PublicationTarget object or URI
An array of PublicationTarget URIs
A PublicationTarget collection object
activateBlueprinting – Indicates whether the item should also be (un-/re-)published in child publications.
activateWorkflow – Indicates whether the item is being (un-/re-)published from the user’s work list.
rollbackOnFailure – Indicates if the entire publish session should be rolled back if a failure occurs while deploying
publishTime – If specified, the item is published (rendered) at the given date/time.
unpublishTime – If specified, the item is un-published at the given date/time. Should be later than publishTime (if specified)
deployTime – If specified, the item is deployed at the given date/time. Should be later than publishTime and earlier than unpublishTime (if specified). If not specified, the item will be deployed on publishTime (i.e. immediately after rendering).
resolveComponentLinks If specified, it resolves the component links. Default is set to true.
priority If specified, it gives a priority on the publish action. Default is set to normal.
ignoreRenderFailures – If specified, it gives the possibility to continue a publish action when there are render failures. Default is set to false.
maximumRenderFailures – If specified, it sets the limit on the number of render failures and ignoreRenderFailures must be set to true.

To further extend your functionality that you can do in Automatic Workflows, you can also create your own classes and functions that are COM visible, and call those methods and objects from your VBScript.  From your .NET code, you can use the Core Services API or even the old TOM API using the interop DLLs.  Remember, although the TOM.NET API has workflow objects, using it in this manner is not supported, and you should stick with one of the other two APIs.