It has been quite sometime since I wrote any blog article. What a way to start SharePoint 2010 blog about Workflows!! It’s all about a new service named Word Automation Services which is a component of SharePoint Server 2010. It provides developers the ability to perform document conversion tasks programmatically through the Word Automation Services application programming interface.
Supposedly we want to create an automation process that will convert bunch of word documents to PDF, XPS or any other format. Once the conversion process is over, then move those documents to other repository. For this we can create a custom workflow activity and then use this in SharePoint designer workflow.
In the custom activity Execute method, use ConversionJob class – http://msdn.microsoft.com/en-us/library/microsoft.office.word.server.conversions.conversionjob.aspx. Make sure that in Central Administration, Word Automation Services is running.
Once we have custom activity built, we need to use it in SharePoint Designer workflow. For this build custom action file and deploy it under ..\14\TEMPLATE\1033\Workflow location
In SharePoint Designer, create a list workflow and choose this custom activity.
In the destination router, choose the Web Service URL that we get from Content Organizer Settings in the site. More details about Content Organizer, http://msdn.microsoft.com/en-us/library/ee558288.aspx. Deploy the workflow in a SharePoint list and the document conversion will happen and then moved to the respective repository.
Caveat using ConversionJob:
In the Execute method of the activity, if the file is not converted (file is null), it moves to else loop wherein we return the activity status as “Executing” i.e ActivityExecutionStatus.Executing. At this point, the workflow instance has no runnable work and is therefore considered idle. At idle-time, the workflow runtime will attempt to persist the state of the workflow instance using a host-provided persistence service. When a workflow instance is idle, its state may be serialized to a host-provided store and removed from memory. Later, The serialized state of the workflow instance can be used to reactivate the instance in memory, at which point the instance is restored to its state and is available to do work.
When the workflow tries to persist the data, we might end up getting this errror, “Unable to serialize workflow, exception: Type ‘Microsoft.Office.Word.Server.Conversions.ConversionJob’ in Assembly ‘Microsoft.Office.Word.Server, Version=22.214.171.124, Culture=neutral, PublicKeyToken=71e9bce111e9429c’ is not marked as serializable”. The reason we get this error is as would have already guessed, the class ‘Microsoft.Office.Word.Server.Conversions.ConversionJob’ is not marked as serializable. So, make sure that any instance of ConversionJob is not declared at class level (as serialization could happen). So use this class only within the respective methods and then dispose them accordingly.