Most of you whom are searching around on this are aware of the solution for SharePoint 2010 (https://msdn.microsoft.com/en-us/library/office/ff181518(v=office.14).aspx). And chances are after you tried on your own to do this for 2013 that there was no List Definition type in VS, right? So after finding this (https://camerondwyer.wordpress.com/2013/09/20/how-to-create-a-custom-sharepoint-list-definition-using-visual-studio-2012/) I decided to frankenstein the old methods with the updated way to do it in Visual Studio. The results of the successful experiment are below.
This article describes the following steps to show how to call the Word Automation Services to convert a document:
- Creating a SharePoint 2013 Empty Project and Adding the SharePoint list.
- Adding a reference to the Microsoft.Office.Word.Server assembly.
- Adding an event receiver.
- Adding the sample code to the solution.
Creating a SharePoint 2013 List Definition (sort of) Application in Visual Studio 2012
This article uses a SharePoint 2013 list definition application for the sample code.
To create a SharePoint 2013 list definition application in Visual Studio 2012
- Start Microsoft Visual Studio 2012 as an administrator.
- In Visual Studio 2012 select File | New Project
- Select Templates | Visual C# | Office/SharePoint | SharePoint 2013 – Empty Project, Provide a name and location for the project/solution and OKName the project and solution something meaningful (ConvertWordToPDF perhaps?).
To create the solution, click OK.
- Select a site to use for debugging and deployment.
- Select the site to use for debugging and the trust level for the SharePoint solution.
|Make sure to select the trust level Deploy as a farm solution. If you deploy as a sandboxed solution, it does not work because the solution uses the Microsoft.Office.Word.Server assembly. This assembly does not allow for calls from partially trusted callers.
- To finish creating the solution, click Finish.
- Once the new solution has been created, we can use the new Visual Designer to create the List Definition. Right click the project in the solution explorer and select Add | New Item
- Select Visual C# Items | Office/SharePoint | List, provide a name and click OK
- Provide a display name for the list. Go with the “Create a customizable list template and a list instance of it” and choose the Document Library.
Adding a Reference to the Microsoft Office Word Server Assembly
To use Word Automation Services, you must add a reference to the Microsoft.Office.Word.Server to the solution.
To add a reference to the Microsoft Office Word Server Assembly
- In Visual Studio, from the Project menu, select Add Reference.
- Locate the assembly. By using the Browse tab, locate the assembly. The Microsoft.Office.Word.Server assembly is located in the SharePoint 2013 ISAPI folder. This is usually located at c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\. After the assembly is located, click OK to add the reference.
Adding an Event Receiver
This article uses an event receiver that uses the Microsoft.Office.Word.Server assembly to create document conversion jobs and add them to the Word Automation Services conversion job queue.
To add an event receiver
- In Visual Studio, on the Project menu, click Add New Item.
- In the Add New Item dialog box, in the Project Templates pane, click the Visual C# SharePoint 2013 template.
- In the Templates pane, click Event Receiver.
- Name the event receiver ConvertWordToPDFEventReceiver and then click Add.
The event receiver converts Word Documents after they are added to the List. Select the An item was added item from the list of events that can be handled.
Click Finish to add the event receiver to the project.
Adding the Sample Code to the Solution
Replace the contents of the ConvertWordToPDFEventReceiver.cs source file with the following code. Just remember to make sure you replace “Word Automation Services” with the name of it in the web applications in central admin.
/// List Item Events
public class ConvertWordToPDFEventReceiver : SPItemEventReceiver
/// An item was added.
public override void ItemAdded(SPItemEventProperties properties)
// Verify the document added is a Word document
// before starting the conversion.
//Variables used by the sample code.
// Initialize the conversion settings.
jobSettings = new ConversionJobSettings();
jobSettings.OutputFormat = SaveFormat.PDF;
// Create the conversion job using the settings.
new ConversionJob("Word Automation Services", jobSettings);
// Set the credentials to use when running the conversion job.
pdfConversion.UserToken = properties.Web.CurrentUser.UserToken;
// Set the file names to use for the source Word document
// and the destination PDF document.
wordFile = properties.WebUrl + "/" + properties.ListItem.Url;
pdfFile = wordFile.Replace(".docx", ".pdf");
pdfFile = wordFile.Replace(".doc", ".pdf");
// Add the file conversion to the conversion job.
// 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.
Deploy and enjoy!