Step 4: Create a Web Application Page to Monitor Conversions

Applies to: SharePoint Server 2010

In this topic you create a SharePoint Server 2010 application page that you use to monitor Word Automation Services document conversions.

Creating an Application Page to Monitor Conversions

The application page that you created in Step 3 of this walkthrough contains a button that starts an application page that allows users to monitor document conversions. The following procedures show how to write code in the application page and its associated .cs "code behind" page that allows users to monitor the status of a document conversion job.

To create the application page

  1. In the Solution Explorer window, right-click on the project, point to Add, and then click New Item…

  2. Select Application Page from the item list.

  3. Enter a name for the application page, for example, ConvertStatus.aspx.

  4. Click Add to add the application page to the project.

    This page specifies three content regions inherited from the SharePoint Server 2010 master page which you override to create the customized application page.

  5. In the HTML for the ConvertStatus.aspx page, locate the <asp:Content> tag with an ID attribute value of PageTitle. Add a string for the page title, for example, Convert Document.

  6. Locate the <asp:Content> tag with an ID attribute value of PageTitleInTitleArea. Add a string for this area of the page, for example, Convert Document.

  7. Locate the <asp:Content> tag with an ID attribute value of Main. Add the following HTML for the page.

    <table cellpadding="5">
    <tr><td><p style="text-align:center">Converting document...</p></td></tr>
    <tr><td><p style="text-align:center"><img src="../images/PROGRESS-CIRCLE-24.GIF" alt="Converting file..." /></p></td></tr>
    <tr><td><p style="text-align:center">This page will automatically refresh once the conversion is complete, or you can return to the document library and the file will be added automatically when available.</p></td></tr>
    <tr><td><p style="text-align:center"></p></td></tr>
    </table>
    
    
  8. In the last row of the table, add a Button control which will allow the user to return to the source location.

    <asp:Button ID="btnReturn" runat="server" Text="Return to Library" />
    
  9. After the code that defines the table, add a Timer control which sets the interval for how often the code checks to see whether the conversion has completed.

    <asp:Timer ID="timer" runat="server" Interval="5000" EnableViewState="True"></asp:Timer>
    

    The Interval property specifies how frequently the timer will execute and refresh the page, in milliseconds. In this case, the value 5000 specifies that the timer will run every 5 seconds.

  10. Leave the PageHead content region blank as the solution does not need to add any content to the <head> tag of the page.

Once you write the HTML code for the ConvertStatus.aspx page, you need to add Visual C# code to the ConvertStatus.aspx.cs code behind page that uses the Word Automation Services.

To add code to the code behind page

  1. In Solution Explorer, expand ConvertItem.aspx, and then double-click ConvertItem.aspx.cs.

  2. Add a using directive for the namespace that contains the Word Automation Services object model that you use to perform document conversions.

    using Microsoft.Office.Word.Server.Conversions;
    
  3. Add an event handler to the ConvertStatus.aspx page that returns the browser to the source location when the button is pressed, by adding the following to the <asp:Button> element.

    OnClick="btnReturn_Click"
    
  4. Next, add the event code to the ConvertStatus.aspx.cs code-behind file for the page.

    protected void btnReturn_Click(object sender, EventArgs e)
    {
        ReturnToLibrary();
    }
    
    private void ReturnToLibrary()
    {
        // Get the URL to go to from the query string, and redirect to there
        string url = Server.UrlDecode(Request.QueryString["url"]);
        Response.Redirect(url);
    }
    

    The previous code retrieves the source URL from the query string and redirects to that location.

  5. Add an event handler to to the ConvertStatus.aspx page that checks the status of the conversion when the timer runs (every 5 seconds), by adding the following to the <asp:Timer> element.

    OnTick="timer_Tick"
    
  6. Next, add the event code to the ConvertStatus.aspx.cs code-behind file for the page.

    protected void timer_Tick(object sender, EventArgs e)
    {
        // Get the job ID and the site subscription ID
        Guid jobId = new Guid(Request.QueryString["jobId"]);
        Guid? siteSubscription = null;
        if(Site.SiteSubscription != null)
            siteSubscription = Site.SiteSubscription.Id;
    
        // Verify that the conversion is complete; if it is, refresh
        ConversionJobStatus status = new ConversionJobStatus("Word Automation Services", jobId, siteSubscription);
    
        if (status.Count == status.Succeeded)
        {
            // Success!
            ReturnToLibrary();
        }
        else if (status.Count == status.Failed)
        {
            // Conversion failed
            // Redirect to standard SharePoint error page
            ReadOnlyCollection<ConversionItemInfo> failedItems = status.GetItems(ItemTypes.Failed);
            Response.Redirect("/_layouts/Error.aspx?ErrorText=" + Server.UrlEncode(failedItems[0].ErrorMessage));
        }
        else if (status.Count == status.Canceled)
        {
            // Conversion was canceled
            // Redirect to standard SharePoint error page
            Response.Redirect("/_layouts/Error.aspx?ErrorText=The conversion was cancelled.");
        }
    
        // It is not finished. Keep waiting.
    }
    

    The previous code retrieves the conversion job ID from the query string and then uses that and the site subscription ID (if present) to check the status of the conversion using the ConversionJobStatus object.

    Note

    This example assumes the name of the service application is "Word Automation Services", which is the default name when it is created by using the Farm Configuration Wizard. If you have used a different name for the service application, you use that instead.

Once the code obtains the conversion job status it checks whether the conversion item reports one of the following states and then takes a corresponding action:

  1. Succeeded – The document was successfully converted. In this case, the code redirects the browser to the source location for the input document.

  2. Failed – The document was not converted. The code retrieves the individual conversion item using the GetItems(ItemTypes) method, reads the ErrorMessage property to determine the cause of the failure, and then uses that information when redirecting to the standard error page.

  3. Canceled – Document conversion was canceled. In this case, the code redirects to the standard error page.

If the conversion job status does not equal one of the previous conversion job statuses, the code returns and waits another 5 seconds before checking the conversion job status.

See Also

Tasks

Step 1: Set Up the Visual Studio 2010 Project for the ECB Menu Item Solution

Step 2: Create the Edit Control Block Menu Item

Step 3: Create a Web Application Page to Start Conversions

Step 5: Build and Deploy the ECB Menu Solution

Concepts

Walkthrough: Use an Edit Control Block Menu Item to Create a Conversion Job