Word Automation Services: Monitoring Status of Conversion Jobs using C#

This is the complete sample for monitoring the status of conversion jobs from the article Developing with SharePoint 2010 Word Automation Services.

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOC

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Word.Server.Conversions;
using System.Threading;

class Program
{
    static void Main(string[] args)
    {
        string siteUrl = "https://localhost";
        // If you manually installed Word Automation Services, then replace the name
        // in the following line with the name that you assigned to the service when
        // you installed it.
        string wordAutomationServiceName = "Word Automation Services";
        using (SPSite spSite = new SPSite(siteUrl))
        {
            Console.WriteLine("Starting conversion job");
            ConversionJob job = new ConversionJob(wordAutomationServiceName);
            job.UserToken = spSite.UserToken;
            job.Settings.UpdateFields = true;
            job.Settings.OutputFormat = SaveFormat.PDF;
            job.Settings.OutputSaveBehavior = SaveBehavior.AlwaysOverwrite;
            SPList listToConvert = spSite.RootWeb.Lists["Shared Documents"];
            job.AddLibrary(listToConvert, listToConvert);
            job.Start();
            Console.WriteLine("Conversion job started");
            ConversionJobStatus status = new ConversionJobStatus(wordAutomationServiceName,
                job.JobId, null);
            Console.WriteLine("Number of documents in conversion job: {0}", status.Count);
            while (true)
            {
                Thread.Sleep(5000);
                status = new ConversionJobStatus(wordAutomationServiceName, job.JobId,
                    null);
      if (status.Count == status.Succeeded + status.Failed)
                {
                    Console.WriteLine("Completed, Successful: {0}, Failed: {1}",
                        status.Succeeded, status.Failed);
                    break;
        }
                Console.WriteLine("In progress, Successful: {0}, Failed: {1}",
                    status.Succeeded, status.Failed);
            }
        }
    }
}