Monday, September 19, 2016

Test the Word Automation Service Application with PowerShell

The Word Automation Services Service Application is a Service Application that automatically converts documents supported by the Word client application into formats such as PDF and XPS. In a simpler way to explain it, the Word Automation Services takes the “Save As” functionality of the Word client, and replicates the functionality on SharePoint.

However, the Word Automation Service Application, does not have a user interface to test that it was configured properly. The only way to actually Test the Word Automation Service Application is trough code, which as a SharePoint IT Professional, I don’t want to write code simply to test a Service Application. Luckily, PowerShell is there for us! By using PowerShell, we can convert a document from Word to PDF, and test that this service application is working properly!

We will first load the assembly required to call Word Automation Services functions.

Add-Type -Path 'C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.Office.Word.Server\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.Office.Word.Server.dll'

We then create new Object of type ConversionJobSettings specifying the Output format is PDF.

$jobSettings = New-Object Microsoft.Office.Word.Server.Conversions.ConversionJobSettings
$jobSettings.OutputFormat = "PDF"

We then create a new object of type ConversionJob and give it our Service Application Proxy Name, in our case Word Automation, as well as the $jobsettings object. We then set the $Job.Usertoken to the SharePoint SPWeb where the document is stored.

 

$job = New-Object Microsoft.Office.Word.Server.Conversions.ConversionJob("Word Automation", $jobSettings)
$job.UserToken = (Get-SPWeb http://ift.tt/2chXCin).CurrentUser.UserToken

Lastly, we add the properties for the file that we want converted. We give it the URL to the Word Document, as well as the URL to the future PDF document, which does not exist yet. We then Start the Job.

$job.AddFile("http://ift.tt/2ckcWji", "http://ift.tt/2chXKOU")
$job.Start()

The Word Automation Services job runs asynchronous, meaning that we won’t know when it will run directly from PowerShell, however we can force the job to run right away by starting the Word Automation timer job.

Start-SPTimerJob "Word Automation"

If we put it all together, the script looks like this:

 

Add-Type -Path 'C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.Office.Word.Server\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.Office.Word.Server.dll'
$jobSettings = New-Object Microsoft.Office.Word.Server.Conversions.ConversionJobSettings
$jobSettings.OutputFormat = "PDF"
$job = New-Object Microsoft.Office.Word.Server.Conversions.ConversionJob("Word Automation", $jobSettings)
$job.UserToken = (Get-SPWeb http://ift.tt/2chXCin).CurrentUser.UserToken
$job.AddFile("http://ift.tt/2ckcWji", "http://ift.tt/2chXKOU")
$job.Start()
Start-SPTimerJob "Word Automation"

To see the status of the job, run the following PowerShell cmdlet where “Word Automation” is the name of your Word Automation Services Application.

new-object Microsoft.Office.Word.Server.Conversions.ConversionJobStatus("Word Automation", $job.JobId,$null);

Initially it will show as InProgress since we force started the Timer Job as seen in the screenshot below.

Test the Word Automation Service Application

Once the job is completed, it will either show as Succeeded or Failed. If everything was configured correctly, it should show as Succeeded as seen in the screenshot below, and you should see your PDF document in your document library.

Test the Word Automation Service Application

Did you find this article useful?

If yes, you will love Deploying SharePoint 2016: Best Practices for Installing, Configuring, and Maintaining SharePoint Server 2016 written by myself, and SharePoint Server MVP Trevor Seward! Get it now at the links below!

The post Test the Word Automation Service Application with PowerShell appeared first on Absolute SharePoint Blog by Vlad Catrinescu.


by Vlad Catrinescu via Absolute SharePoint Blog by Vlad Catrinescu

No comments:

Post a Comment