​So in my recent escapades with SharePoint 2010 I’ve been trying to play with the new Word Automation Services.  Microsoft was so kind to release a demo feature receiver that shows how you can quite easily use the new service to convert Microsoft Word documents to PDF documents when a document is added to a document library, but using that as a starting point you can theoritically convert them whenever you want tyou can easily adapt the sample to convert whenever and wherever you like in your custom SharePoint applications.

Now for the life of me I couldn’t get this to work.  The event receiver would fire, everything looked great, no error messages in the Event Viewer or in the ULS logs, so I assumed I’d just have to wait, and wait, and wait some more.  But alas nothing was happening.  It appeared as though the Word Automation Services was just not working.

So it was time to breakout LINQPad.  For those not familiar with LINQPad, it’s a great asset for any developers toolbox that allows you to quickly execute .NET code.  I especially like this tool when trying to test out snippets of code against SharePoint that aren’t working and I’m making lots of changes, as opposed to having to build and compile my code, deploy it to SharePoint, activate the features, and then run it everytime I make a change, LINQPad allows me to just run the code.  Anyways enough about LINQPad.

So using LINQPad I ported the event receiver code from Microsoft over, and I found another chunk of code that allows you to monitor the conversion status of the job.  Here’s the code I used to convert the file, and monitor the status of the job.

 

// Create the site context
using (SPSite site = new SPSite(siteUrl))
{
	//Variables used by the sample code.
	ConversionJobSettings jobSettings;
	ConversionJob pdfConversion;
	string wordFile;
	string pdfFile;
	
	// Get the site context
	SPServiceContext context = SPServiceContext.GetContext(site);
	
	// Create the conversion job using the settings.
	var wordAutomationProxy = (WordServiceApplicationProxy)context.GetDefaultProxy(typeof(WordServiceApplicationProxy));
	
	// Initialize the conversion settings.
	jobSettings = new ConversionJobSettings();
	jobSettings.OutputFormat = SaveFormat.PDF;
		
	pdfConversion = new  ConversionJob (wordAutomationProxy, jobSettings);
	
	// Set the credentials to use when running the conversion job.
	pdfConversion.UserToken = site.RootWeb.CurrentUser.UserToken;
		
	// Set the file names to use for the source Word document
	// and the destination PDF document.
	wordFile = inputFileUrl;
	pdfFile = outputFileUrl;
	
	// Add the file conversion to the conversion job.
	pdfConversion.AddFile(wordFile, pdfFile);
	
	// Add the conversion job to the Word Automation Services 
	// conversion job queue. The conversion does not occur
	// immediately but is processed during the next run of
	// the document conversion job.
	pdfConversion.Start();
	
	Console.WriteLine("Conversion job started");
	ConversionJobStatus status = new ConversionJobStatus(wordAutomationProxy, pdfConversion.JobId, null);

	Console.WriteLine("Number of documents in conversion job: {0}", status.Count);

	while (true)
	{
		Thread.Sleep(5000);
		status = new ConversionJobStatus(wordAutomationProxy, pdfConversion.JobId, null);
		if (status.Count == status.Succeeded + status.Failed)
		{
			Console.WriteLine("Completed, Successful: {0}, Failed: {1}",
				status.Succeeded, status.Failed);
			ReadOnlyCollection failedItems =
				status.GetItems(ItemTypes.Failed);
			foreach (var failedItem in failedItems)
			{
				Console.WriteLine("Failed item: Name:{0}", failedItem.InputFile);
				failedItem.ErrorMessage.Dump();
				failedItem.ErrorCode.Dump();
			}
				
			break;
		}
		
		Console.WriteLine("In progress, Successful: {0}, Failed: {1}", 
			status.Succeeded, status.Failed);
	}
}

So after running this I got the following error message:

“The file could not be converted; it may be corrupt or otherwise invalid (the conversion process failed). Please try opening the file in Microsoft Word, resaving it, and then resubmitting the file for conversion. If this does not resolve the issue, contact your system administrator.”

Error Code: 65543

So off to the internet to find out what this error code means, and if you’re here, you probably found very little aside from the actual error message that the error code corresponds too, or vice versa.  So after several hours of reading through various information regarding permissions on accounts, databases, and sites, I finally stumbled upon a blog post by Jonation Beckett at nintex blogs.  He had mentioned that after SP1 was released, the Word Automation Service had been set to run in Sandboxed mode.  What does that mean, and why would Microsoft change it, I don’t know, but I’d love to find out.  So he proposed disabling this by running the following PowerShell commands:

 

$sp = Get-SPServiceApplication | where {$_.TypeName.Equals("Word Automation Services")}  

$sp.DisableSandbox = $true 

$sp.Update()

So I tried this, but still nothing, and still the same error message when I re-ran the code.  But more so out of sheer anger than for any other reason I restarted the SharePoint Timer Service and issues an IISRESET command to see if cycling these would maybe pick up the changes made to the Word Automation Service.  ALAS, it did, and it worked, my first document was converted successfully.  Hopefully this will help someone else out, and if you have any insights as to why the Word Automation Service was changed to run in Sandboxed mode, please let me know.

**You can find the source code for the Microsoft Event Receiver here:

http://msdn.microsoft.com/en-us/library/ff181518.aspx

Posted by Chris Buchanan

Leave a reply

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