步骤 4:创建 Web 应用程序页以监视转换

上次修改时间: 2010年4月6日

适用范围: SharePoint Server 2010

在本主题中,将创建一个用于监视 Word Automation Services 文档转换的 SharePoint Server 2010 应用程序页。

创建用于监视转换的应用程序页

在本演练的步骤 3 中创建的应用程序页包含一个按钮,该按钮可启动允许用户监视文档转换的应用程序页。以下过程说明如何在应用程序页及其关联的 .cs"代码隐藏"页(该页允许用户监视文档转换作业的状态)中编写代码。

创建应用程序页

  1. 在"解决方案资源管理器"窗口中,右键单击项目,指向"添加",然后单击"新建项目"。

  2. 从项列表中选择"应用程序页"。

  3. 为应用程序页输入名称,例如 ConvertStatus.aspx。

  4. 单击"添加"将应用程序页添加到项目。

    该页指定了三个继承自 SharePoint Server 2010 母版页的内容区域,可重写这些区域以创建自定义应用程序页。

  5. 在 ConvertStatus.aspx 页的 HTML 中,找到 ID 属性值为 PageTitle 的 <asp:Content> 标 记。为页标题添加字符串,例如 Convert Document。

  6. 找到 ID 属性值为 PageTitleInTitleArea 的 <asp:Content> 标 记。为页的该区域添加字符串,例如 Convert Document。

  7. 找到 ID 属性值为 Main 的 <asp:Content> 标 记。为页添加以下 HTML。

    <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. 在表的最后一行中,添加一个"按钮"控件,用户可利用此控件返回到源位置。

    <asp:Button ID="btnReturn" runat="server" Text="Return to Library" />
    
  9. 在定义表的代码后添加一个"计时器"控件,该控件可设置代码检查的时间间隔以查看是否已完成转换。

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

    此时间间隔属性指定计时器将执行和刷新页的频率(以毫秒为单位)。在此示例中,值 5000 指定计时器将每 5 秒运行一次。

  10. 当解决方案不需要向页的 <head> 标记添加任何内容时,请保持 PageHead 内容区域为空。

在为 ConvertStatus.aspx 页编写 HTML 代码后,您需要将 Visual C# 代码添加到使用 Word Automation Services 的 ConvertStatus.aspx.cs 代码隐藏页。

将代码添加到代码隐藏页

  1. 在"解决方案资源管理器"中,展开"ConvertItem.aspx",然后双击"ConvertItem.aspx.cs"。

  2. 为包含用于执行文档转换的 Word Automation Services 对象模型的命名空间添加 using 指令。

    using Microsoft.Office.Word.Server.Conversions;
    
  3. 在按此按钮时,将通过向 <asp:Button> 元素添加以下代码,为将浏览器返回到源位置的 ConvertStatus.aspx 页添加事件处理程序。

    OnClick="btnReturn_Click"
    
  4. 接下来,将事件代码添加到该页的 ConvertStatus.aspx.cs 代码隐藏文件。

    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);
    }
    

    上一代码从查询字符串检索源 URL 并重定向到该位置。

  5. 当计时器运行(每 5 秒运行一次)时,将通过向 <asp:Timer> 元素添加以下代码,为用于检查转换状态的 ConvertStatus.aspx 页添加事件处理程序。

    OnTick="timer_Tick"
    
  6. 接下来,将事件代码添加到该页的 ConvertStatus.aspx.cs 代码隐藏文件。

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

    上一代码从查询字符串检索转换作业 ID,然后使用此 ID 和网站订阅 ID(如果有)检查使用 ConversionJobStatus 对象的转换的状态。

    备注

    此示例假定服务应用程序的名称为"Word Automation Services",该名称是在使用服务器场配置向导创建服务应用程序时其具有的默认名称。如果已为服务应用程序使用一个不同的名称,则改用此名称。

在代码获取转换作业状态后,它会检查转换项是否报告下列状态之一,然后执行相应操作:

  1. 已成功 – 已成功转换文档。在此情况下,代码会将浏览器重定向到输入文档的源位置。

  2. 已失败 – 未转换文档。代码会检索使用 GetItems(ItemTypes) 方法的单个转换项,读取 ErrorMessage 属性以确定失败原因,然后在重定向到标准错误页时使用该信息。

  3. 已取消 – 已取消文档转换。在此情况下,代码会重定向到标准错误页。

如果转换作业状态不是以前的转换作业状态之一,则代码将返回,并在检查转换作业状态之前再等待 5 秒。

请参阅

任务

步骤 1:针对 ECB 菜单项解决方案设置 Visual Studio 2010 项目

步骤 2:创建编辑控制块菜单项

步骤 3:创建 Web 应用程序页以开始转换

步骤 5:生成并部署 ECB 菜单解决方案

概念

演练:使用编辑控制块菜单项创建转换作业