Jan16

Keep the Migraine Out Of Your SharePoint Migration – Part Three – Using Word Automation Services

In the first part of this series, we covered how a SharePoint Migration can be a frustrating experience.  In part two, we continued with that theme and spoke to how the Word Automation Service itself can cause you some issues.  We encourage you to review those two posts before reading the gripping conclusion!

Taming Word Automation Services

Presuming that you have read the other two posts in this series, we should all be on the same page as far as what we’d like to do with the Word Automation Services (WAS), but the question remains: How do we get WAS to do what we want?

We’re going to tackle that head-on by covering the steps needed to set up the foundation for a successful script using

SharePoint and Word Automation Service

Calm Down! Word Automation Services in SharePoint doesn’t have to hurt!

PowerShell.  The goal being to use WAS in SharePoint to convert all documents in a folder from one format to another.  In this case we’ll focus on converting MS Word docs to Adobe PDFs.

Before we dig in, be sure that you have a connection established within SharePoint for the Word Automation Services.  In the code that follows, please note the elements denoted by <~ ~> are intended to show that you will need to add information from your specific SharePoint environment within that element.  You can use this handy cheat sheet to determine what you’ll need to supply:

PS – Name of Proxy Service
SPURL – URL of SharePoint web
LOC.IN – Location of file or folder containing the original document(s)
LOC.OUT – Location of file or folder to deposit the converted file(s)

#Connect to Word Automation Services
$wasp = Get-SPServiceApplicationProxy | where { $_.TypeName -eq "<~PS~>" }
$job = New-Object Microsoft.Office.Word.Server.Conversions.ConversionJob($wasp)

Presuming that you’re still following along, the next step is to access the site which contains the documents in question.  Be sure to insert the specific information needed in the locations indicated in the code below.

# get the site, folder containing documents to be converted, and folder to store the converted documents
$web = Get-SPWeb <~SPURL~>
$inputFolder = $web.GetFolder("<~LOC.IN~>")
$outputFolder = $web.GetFolder("<~LOC.OUT~>")

Now we need to create a custom job to perform the conversion as well as to set preferences as to how the service should complete the process.

These preferences include the final format of the document(s) as well as to specify how the documents are handled during and after the conversion process.  You will have to decide exactly what options are important to you, so you may need to do some research on that score.  Still, here is some code to get you started on a simple conversion.

#set up conversion job
$job.UserToken = $web.CurrentUser.UserToken
$job.Name = "ConvertAll: Docx to PDF"
$job.Settings.OutputFormat = [Microsoft.Office.Word.Server.Conversions.SaveFormat]::PDF
$job.Settings.OutputSaveBehavior = [Microsoft.Office.Word.Server.Conversions.SaveBehavior]::AlwaysOverwrite

Next up is a method to select the source and destination folders, and then to initiate the conversion job.  You can also use Central Administration to amend the frequency of the job you’re running.

#Add input and output folders and start conversion"
$job.AddFolder($inputFolder, $outputFolder, $false)
$job.Start()

Lastly we want to share a bit of code that will provide a final report on the times converted.  We do like to be able to verify this stuff, just to be sure!

#view status of job
$status = New-Object Microsoft.Office.Word.Server.Conversions.ConversionJobStatus($wasp.Id, $job.JobId, $null)
$status

We hope that this technique and the other two parts of this series serve to help alleviate your migration migraines.  Let us know how this worked out for you in the comments section below, and if you found it helpful, please share across your social media channels.

Leave a Comment

Your email address will not be published. Required fields are marked *