本演练演示如何将显示派生自工作流的数据的应用程序页添加到工作流项目中。本演练是基于演练:创建带有关联窗体和启动窗体的工作流主题中描述的项目构建的。
本演练将演示以下任务:
将 ASPX 应用程序页添加到 SharePoint 工作流项目中。
从工作流项目中获取数据并操作数据。
在应用程序页上的表中显示数据。
![]() |
---|
对于在以下说明中使用的某些 Visual Studio 用户界面元素,您的计算机可能会显示不同的名称或位置。这些元素取决于您所使用的 Visual Studio 版本和您所使用的设置。有关更多信息,请参见 Visual Studio 设置。 |
系统必备
您需要以下组件来完成本演练:
支持的 Microsoft Windows 和 SharePoint 版本。有关更多信息,请参见开发 SharePoint 解决方案的要求。
Visual Studio。
还必须完成演练:创建带有关联窗体和启动窗体的工作流主题中的项目。
修改工作流代码
首先,向工作流中添加一行代码,以将“Outcome”(结果)列的值设置为零用金报销单上显示的金额。以后会在零用金报销单汇总计算中用到此值。
设置工作流中的“Outcome”(结果)列的值
将在演练:创建带有关联窗体和启动窗体的工作流主题中完成的项目加载到 Visual Studio。
打开 Workflow1.cs 代码或 Workflow1.vb 代码(具体取决于您的编程语言)。
将以下代码添加到 createTask1_MethodInvoking 方法的底部:
createTask1_TaskProperties1.ExtendedProperties("Outcome") = workflowProperties.InitiationData
createTask1_TaskProperties1.ExtendedProperties["Outcome"] = workflowProperties.InitiationData;
创建应用程序页
接下来,向项目中添加 ASPX 窗体。此窗体将显示从零用金报销单工作流项目中获取的数据。为此,将添加一个应用程序页。应用程序页使用与其他 SharePoint 页相同的母版页,这意味着应用程序页将与 SharePoint 网站上的其他页类似。
向项目中添加应用程序页
选择ExpenseReport项目,然后,在菜单栏上,选择 项目,添加新项目。
在 模板 窗格中,选择 应用程序页 模板,为项目项(ApplicaitonPage1.aspx)使用默认名称,然后选择 添加 按钮。
在 ApplicationPage1.aspx 的 XML 中,用以下代码替换 PlaceHolderMain 部分:
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server"> <asp:Label ID="Label1" runat="server" Font-Bold="True" Text="Expenses that exceeded allotted amount" Font-Size="Medium"></asp:Label> <br /> <asp:Table ID="Table1" runat="server"> </asp:Table> </asp:Content>
此代码将表和标题一起添加到页面中。
通过用以下代码替换 PlaceHolderPageTitleInTitleArea 部分,将标题添加到应用程序页中:
<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" > Expense Report Summary </asp:Content>
编写应用程序页代码
接下来,向零用金报销单汇总应用程序页中添加代码。当打开该页时,代码将扫描 SharePoint 中的任务列表,以找出超出分配的开支限制的零用金。报表将每一项与零用金总计一起列出。
编写应用程序页代码
选择 ApplicationPage1.aspx 节点,然后,在菜单栏上,选择 视图,代码 显示应用程序页背后的代码。
用以下代码替换类顶部的 using 或 Import 语句(具体取决于您的编程语言):
Imports System Imports Microsoft.SharePoint Imports Microsoft.SharePoint.WebControls Imports System.Collections Imports System.Data Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Web.UI.WebControls.WebParts Imports System.Drawing Imports Microsoft.SharePoint.Navigation
using System; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using System.Collections; using System.Data; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Drawing; using Microsoft.SharePoint.Navigation;
将以下代码添加到 Page_Load 方法中:
Try ' Reference the Tasks list on the SharePoint site. ' Replace "TestServer" with a valid SharePoint server name. Dim site As SPSite = New SPSite("http://TestServer") Dim list As SPList = site.AllWebs(0).Lists("Tasks") ' string text = ""; Dim sum As Integer = 0 Table1.Rows.Clear() ' Add table headers. Dim hr As TableHeaderRow = New TableHeaderRow hr.BackColor = Color.LightBlue Dim hc1 As TableHeaderCell = New TableHeaderCell Dim hc2 As TableHeaderCell = New TableHeaderCell hc1.Text = "Expense Report Name" hc2.Text = "Amount Exceeded" hr.Cells.Add(hc1) hr.Cells.Add(hc2) ' Add the TableHeaderRow as the first item ' in the Rows collection of the table. Table1.Rows.AddAt(0, hr) ' Iterate through the tasks in the Task list and collect those ' that have values in the "Related Content" and "Outcome" fields ' - the fields written to when expense approval is required. For Each item As SPListItem In list.Items Dim s_relContent As String = "" Dim s_outcome As String = "" Try ' Task has the fields - treat as expense report. s_relContent = item.GetFormattedValue("Related Content") s_outcome = item.GetFormattedValue("Outcome") Catch erx As System.Exception ' Task does not have fields - skip it. Continue For End Try ' Convert amount to an int and keep a running total. If (Not String.IsNullOrEmpty(s_relContent) And Not String.IsNullOrEmpty(s_outcome)) Then sum = (sum + Convert.ToInt32(s_outcome)) Dim relContent As TableCell = New TableCell relContent.Text = s_relContent Dim outcome As TableCell = New TableCell outcome.Text = ("$" + s_outcome) Dim dataRow2 As TableRow = New TableRow dataRow2.Cells.Add(relContent) dataRow2.Cells.Add(outcome) Table1.Rows.Add(dataRow2) End If Next ' Report the sum of the reports in the table footer. Dim tfr As TableFooterRow = New TableFooterRow tfr.BackColor = Color.LightGreen ' Create a TableCell object to contain the ' text for the footer. Dim ftc1 As TableCell = New TableCell Dim ftc2 As TableCell = New TableCell ftc1.Text = "TOTAL: " ftc2.Text = ("$" + Convert.ToString(sum)) ' Add the TableCell object to the Cells ' collection of the TableFooterRow. tfr.Cells.Add(ftc1) tfr.Cells.Add(ftc2) ' Add the TableFooterRow to the Rows ' collection of the table. Table1.Rows.Add(tfr) Catch errx As Exception System.Diagnostics.Debug.WriteLine(("Error: " + errx.ToString)) End Try
try { // Reference the Tasks list on the SharePoint site. // Replace "TestServer" with a valid SharePoint server name. SPSite site = new SPSite("http://TestServer"); SPList list = site.AllWebs[0].Lists["Tasks"]; // string text = ""; int sum = 0; Table1.Rows.Clear(); // Add table headers. TableHeaderRow hr = new TableHeaderRow(); hr.BackColor = Color.LightBlue; TableHeaderCell hc1 = new TableHeaderCell(); TableHeaderCell hc2 = new TableHeaderCell(); hc1.Text = "Expense Report Name"; hc2.Text = "Amount Exceeded"; hr.Cells.Add(hc1); hr.Cells.Add(hc2); // Add the TableHeaderRow as the first item // in the Rows collection of the table. Table1.Rows.AddAt(0, hr); // Iterate through the tasks in the Task list and collect those // that have values in the "Related Content" and "Outcome" // fields - the fields written to when expense approval is // required. foreach (SPListItem item in list.Items) { string s_relContent = ""; string s_outcome = ""; try { // Task has the fields - treat as expense report. s_relContent = item.GetFormattedValue("Related Content"); s_outcome = item.GetFormattedValue("Outcome"); } catch { // Task does not have fields - skip it. continue; } if (!String.IsNullOrEmpty(s_relContent) && !String.IsNullOrEmpty(s_outcome)) { // Convert amount to an int and keep a running total. sum += Convert.ToInt32(s_outcome); TableCell relContent = new TableCell(); relContent.Text = s_relContent; TableCell outcome = new TableCell(); outcome.Text = "$" + s_outcome; TableRow dataRow2 = new TableRow(); dataRow2.Cells.Add(relContent); dataRow2.Cells.Add(outcome); Table1.Rows.Add(dataRow2); } } // Report the sum of the reports in the table footer. TableFooterRow tfr = new TableFooterRow(); tfr.BackColor = Color.LightGreen; // Create a TableCell object to contain the // text for the footer. TableCell ftc1 = new TableCell(); TableCell ftc2 = new TableCell(); ftc1.Text = "TOTAL: "; ftc2.Text = "$" + Convert.ToString(sum); // Add the TableCell object to the Cells // collection of the TableFooterRow. tfr.Cells.Add(ftc1); tfr.Cells.Add(ftc2); // Add the TableFooterRow to the Rows // collection of the table. Table1.Rows.Add(tfr); } catch (Exception errx) { System.Diagnostics.Debug.WriteLine("Error: " + errx.ToString()); }
警告
确保将运行SharePoint有效的服务器名称替换“TestServer”在代码。
测试应用程序页
接下来,确定应用程序页是否正确显示零用金数据。
测试应用程序页
选择F5键运行并将项目部署到SharePoint。
选择 主页 按钮,然后在快速启动栏中的 共享文档 链接显示"共享文档"列表在SharePoint站点。
表示此示例的零用金报销单),一些新文档到文档通过选中 LibraryTools 选项的 文档 链接在页面顶部然后在工具功能区的 上载文档 按钮列表中上载。
在上载一些文档后,通过选择在 LibraryTools 选项的 库 链接在页面顶部然后在工具功能区的 库设置 按钮实例化工作流。
在 文档库设置 页上,选择在 权限和管理 部分的 工作流设置 链接。
在 工作流设置 页上,选择 添加工作流 链接。
在 添加工作流 页上,选择 ExpenseReport - Workflow1 工作流,输入名称工作流,例如ExpenseTest,然后选择 接下来 按钮。
将显示工作流的关联窗体。将使用此窗体来报告零用金限制金额。
在关联窗体中,输入 1000自动审批限额 框,然后选择 关联工作流 按钮。
选择 主页 按钮返回到SharePoint主页。
选择在快速启动栏上的 共享文档 链接。
选择上载的文档以显示下拉箭头,选择该文件,然后选择 工作流 项目。
在ExpenseTest旁边选择图像显示工作流启动窗体。
在 费用总额 文本框中,输入大于1000的值,然后选择 启动工作流 按钮。
如果报告的费用超过分配的零用金,则将向任务列表中添加任务。具有**“已完成”值的名为“ExpenseTest”**的列也将添加到“共享文档”列表中的零用金报销单项中。
对于“共享文档”列表中的其他文档,重复步骤 11 至步骤 13。(文档的具体数目无关紧要。)
通过在 Web 浏览器中打开以下 URL 来显示零用金报销单汇总应用程序页:http://系统名称/_layouts/ExpenseReport/ApplicationPage1.aspx。
零用金报销单汇总页将列出超过分配的金额的所有零用金报销单、报销单超出的金额以及所有报销单的总金额。
后续步骤
有关 SharePoint 应用程序页的更多信息,请参见为 SharePoint 创建应用程序页。
通过按照以下主题的说明在 Visual Studio 中使用 Visual Web Designer,可以了解有关如何设计 SharePoint 页面内容的更多信息: