Step 2: Code the Web Part
Last modified: July 20, 2010
Applies to: SharePoint Server 2010
This topic shows how to write code that initiates Word Automation Services document conversion using a SharePoint Server 2010 Web Part. An end user can convert documents in the specified SharePoint Document Library by clicking a button on a Web Part.
The following procedures add a button to the Web Part and then add code in the Click event for the button that initiates a Word Automation Services document conversion.
To add a button to the Web part
In the Visual Studio 2010 project for the Web Part solution example, in Solution Explorer, expand VisualWebPart1, right-click VisualWebPart1UserControl.ascx and then click View Designer.
From Toolbox, drag a button control to the designer surface.
With the button selected in the designer, in Properties, double-click in the (ID) field, type btnSubmit, and then press ENTER.
In Properties, scroll down to Text, double-click the Text field, type Convert Document Library, and then press ENTER.
To convert the documents
With VisualWebPart1UserControl.ascx open in the designer, ensure that the button on the Web Part surface is selected.
In Properties, click the button with the lightning bolt icon to move to the Events view, and then double-click in the Click event field. The btnSubmit_Click event handler is created in VisualWebPart1UserControl.ascx.cs.
In VisualWebPart1UserControl.ascx.cs, add a using directive for Word Automation Services.
The primary objects used to perform Word Automation Services document conversion are in the Microsoft.Office.Word.Server.Conversions namespace.
In the btnSubmit_Click method, add the following code.
The string argument passed to the ConversionJob constructor must be the name of the service application instance for Word Automation Services, as described in the topic Configuring Word Automation Services for Development. The service application instance name is visible on the Manage Service Applications page in SharePoint Central Administration.
Next, set properties on the ConversionJob by adding the following code.
myJob.Settings.OutputFormat = SaveFormat.PDF; myJob.Settings.OutputSaveBehavior = SaveBehavior.AppendIfPossible;
The two settings in the code specify:
The output format should be in PDF format.
The output files should be appended as a new version to existing files when versioning is turned on, and replace those existing files otherwise.
Set the credentials to present when running the conversion job.
This setting specifies that all reading/writing of documents should use the credentials of the user that clicks the button. By default, Word Automation Services uses the anonymous context, so it is important to set this property.
Next, specify the input library that contains the files to convert and the output library for the converted files.
Finally, complete the code for the btnSubmit_Click method by adding code that starts the Word Automation Services conversion job.
When a user clicks the button on the Web Part, the code initiates a conversion job for the files in the input library.