本演练演示如何使用 Visual Studio 为网站级工作流创建自定义活动。(网站级工作流适用于整个网站,而不只是网站上的列表。)自定义活动会创建一个备份的公告列表,然后将公告列表中的内容复制到该列表中。
本演练将演示以下任务:
创建网站级工作流。
创建自定义工作流活动。
创建和删除 SharePoint 列表。
将项从一个列表复制到另一个列表。
在快速启动栏上显示列表。
说明 |
|---|
对于在以下说明中使用的某些 Visual Studio 用户界面元素,您的计算机可能会显示不同的名称或位置。这些元素取决于您所使用的 Visual Studio 版本和您所使用的设置。有关更多信息,请参见 Visual Studio 设置。 |
系统必备
您需要以下组件来完成本演练:
支持的 Microsoft Windows 和 SharePoint 版本。有关更多信息,请参见开发 SharePoint 解决方案的要求。
Visual Studio。
创建网站工作流自定义活动项目
首先,创建一个用来包含和测试自定义工作流活动的项目。
创建网站工作流自定义活动项目
在菜单栏上,依次选择 文件,新,项目 显示 新项目 对话框。
外接 SharePoint 节点。visual C# 或 Visual Basic下,然后选择 2010年 节点。
在 模板 窗格中,选择 SharePoint 2010项目 模板。
在 名称 框中,键入AnnouncementBackup,然后选择 好 按钮。
这将显示**“SharePoint 自定义向导”**。
在 指定用于调试的网站和安全级别 页上,选择 部署为场解决方案 选项按钮,然后选择 完成 按钮接受信任级别和默认网站。
此步骤会将解决方案的信任级别设置为场解决方案(工作流项目的唯一可用选项)。
在 解决方案资源管理器,选择项目节点,然后,在菜单栏上,选择 项目,添加新项目。
在 visual C# 或 Visual Basic下,展开 SharePoint 节点,然后选择 2010年 节点。
在 模板 窗格中,选择 顺序工作流(仅场解决方案) 模板,然后选择 添加 按钮。
这将显示**“SharePoint 自定义向导”**。
在 指定用于调试工作流名称 页上,接受默认名称(AnnouncementBackup - Workflow1)。将工作流模板类型到 网站工作流,然后选择 接下来 按钮。
选择 完成 按钮以接受剩余的默认设置。
添加自定义工作流活动类
接下来,向项目中添加一个类以包含自定义工作流活动的代码。
添加自定义工作流活动类
在菜单栏上,依次选择 项目,添加新项目 显示 添加新项目 对话框。
在 安装的模板 树视图中,选择 代码 节点,然后在项目项模板列表中 选件类 模板。使用默认名称 Class1。选择**“添加”**按钮。
将 Class1 中的所有代码替换为:
Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Text Imports Microsoft.SharePoint Namespace AnnouncementBackup ' This custom activity will back up all of the announcements ' in the Announcements list on the SharePoint site. Public Class Class1 Inherits System.Workflow.ComponentModel.Activity Public Sub New() MyBase.New() End Sub ' Triggers when the activity is executed. Protected Overrides Function Execute(ByVal executionContext As System.Workflow.ComponentModel.ActivityExecutionContext) As System.Workflow.ComponentModel.ActivityExecutionStatus Try ' Get a reference to the SharePoint site. Dim site As SPSite = New SPSite(("http://" + System.Environment.MachineName)) Dim web As SPWeb = site.OpenWeb("/") ' Reference the original Announcements list. Dim aList As SPList = web.GetList("/Lists/Announcements") ' If the Announcements Backup list already exists, delete it. Try Dim bList As SPList = web.GetList("/Lists/Announcements Backup") bList.Delete() Catch End Try ' Create a new backup Announcements list and reference it. Dim newAnnID As Guid = web.Lists.Add("Announcements Backup", "A backup Announcements list.", SPListTemplateType.Announcements) Dim bakList As SPList = web.Lists(newAnnID) ' Copy announcements from original to backup Announcements list. For Each item As SPListItem In aList.Items Dim newAnnItem As SPListItem = bakList.Items.Add For Each field As SPField In aList.Fields If Not field.ReadOnlyField Then newAnnItem(field.Id) = item(field.Id) End If Next newAnnItem.Update() Next ' Put the Backup Announcements list on the QuickLaunch bar. bakList.OnQuickLaunch = True bakList.Update() Catch errx As Exception System.Diagnostics.Debug.WriteLine(("Error: " + errx.ToString)) End Try Return MyBase.Execute(executionContext) End Function End Class End Namespaceusing System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SharePoint; namespace AnnouncementBackup { // This custom activity will back up all of the announcements in // the Announcements list on the SharePoint site. public class Class1 : System.Workflow.ComponentModel.Activity { public Class1() { } // Triggers when the activity is executed. protected override System.Workflow.ComponentModel.ActivityExecutionStatus Execute(System.Workflow.ComponentModel.ActivityExecutionContext executionContext) { try { // Get a reference to the SharePoint site. SPSite site = new SPSite("http://" + System.Environment.MachineName); SPWeb web = site.OpenWeb("/"); // Reference the original Announcements list. SPList aList = web.GetList("/Lists/Announcements"); // If the Announcements Backup list already exists, delete it. try { SPList bList = web.GetList("/Lists/Announcements Backup"); bList.Delete(); } catch { } // Create a new backup Announcements list and reference it. Guid newAnnID = web.Lists.Add("Announcements Backup", "A backup Announcements list.", SPListTemplateType.Announcements); SPList bakList = web.Lists[newAnnID]; // Copy announcements from original to backup Announcements list. foreach (SPListItem item in aList.Items) { SPListItem newAnnItem = bakList.Items.Add(); foreach (SPField field in aList.Fields) { if (!field.ReadOnlyField) newAnnItem[field.Id] = item[field.Id]; } newAnnItem.Update(); } // Put the Backup Announcements list on the QuickLaunch bar. bakList.OnQuickLaunch = true; bakList.Update(); } catch (Exception errx) { System.Diagnostics.Debug.WriteLine("Error: " + errx.ToString()); } return base.Execute(executionContext); } } }保存项目,然后,在菜单栏上,选择 生成,生成解决方案。
Class1将为 工具箱 的自定义操作在 AnnouncementBackup元素 选项。
向网站工作流中添加自定义活动
接下来,向工作流中添加一个活动以包含自定义代码。
向网站工作流中添加自定义活动
在设计视图中,在工作流设计器内打开 Workflow1。
从拖到 工具箱 的Class1,以使它在 onWorkflowActivated1 事件下,或者打开Class1的快捷菜单,选择 复制,打开行的快捷菜单在 onWorkflowActivated1 活动下方,然后选择 粘贴。
保存项目。
测试网站工作流自定义活动
紧接着,运行项目并启动网站工作流。自定义活动会创建一个备份的公告列表,然后将当前公告列表中的内容复制到该列表中。在创建备份列表之前,代码还会检查是否已存在备份列表。如果已存在备份列表,则会将其删除。代码还会向 SharePoint 网站的快速启动栏上的新列表中添加链接。
测试网站工作流自定义活动
选择F5键运行项目并将其部署到SharePoint。
在快速启动栏上,选择 列表 链接以显示可用在SharePoint网站的所有列表。请注意,仅有一个名为**“公告”**的公告列表。
在SharePoint网页顶部,选择 网站工作流 链接。
在开始下一个新工作流"部分,选择 AnnouncementBackup – Workflow1 链接。这将启动网站工作流,并运行自定义操作中的代码。
在快速启动栏上,选择 备份的公告 链接。请注意,**“公告”**列表中包含的所有公告已复制到此新列表中。
说明