Step 2: Code the Web Part

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.

Write Code to Convert the Document Library

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

  1. 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.

  2. From Toolbox, drag a button control to the designer surface.

  3. With the button selected in the designer, in Properties, double-click in the (ID) field, type btnSubmit, and then press ENTER.

  4. In Properties, scroll down to Text, double-click the Text field, type Convert Document Library, and then press ENTER.

To convert the documents

  1. With VisualWebPart1UserControl.ascx open in the designer, ensure that the button on the Web Part surface is selected.

  2. 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.

  3. In VisualWebPart1UserControl.ascx.cs, add a using directive for Word Automation Services.

    using Microsoft.Office.Word.Server.Conversions;
    

    The primary objects used to perform Word Automation Services document conversion are in the Microsoft.Office.Word.Server.Conversions namespace.

  4. In the btnSubmit_Click method, add the following code.

    ConversionJob myJob = new ConversionJob("Word Automation Services");
    

    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.

  5. 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.

  6. Set the credentials to present when running the conversion job.

    Important

    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.

    myJob.UserToken = SPContext.Current.Web.CurrentUser.UserToken;
    
  7. Next, specify the input library that contains the files to convert and the output library for the converted files.

    SPWeb myWebSite = SPContext.Current.Web;
       SPList inputLibrary = myWebSite.Lists["Input"];
       SPList outputLibrary = myWebSite.Lists["Output"];
       myJob.AddLibrary(inputLibrary, outputLibrary, true);
    
  8. Finally, complete the code for the btnSubmit_Click method by adding code that starts the Word Automation Services conversion job.

    myJob.Start();
    

    When a user clicks the button on the Web Part, the code initiates a conversion job for the files in the input library.

See Also

Tasks

Step 1: Set Up the Visual Studio 2010 Project For the Web Part Solution

Step 3: Build and Deploy the Web Part Solution

Concepts

Walkthrough: Creating a Web Part That Uses Word Automation Services