演练:创建自定义网站工作流活动

本演练演示如何使用 Visual Studio 为网站级工作流创建自定义活动。 (网站级工作流适用于整个网站,而不只是网站上的列表。)自定义活动会创建一个备份的公告列表,然后将公告列表中的内容复制到该列表中。

本演练将演示以下任务:

  • 创建网站级工作流。

  • 创建自定义工作流活动。

  • 创建和删除 SharePoint 列表。

  • 将项从一个列表复制到另一个列表。

  • 在快速启动栏上显示列表。

提示

对于在以下说明中使用的某些 Visual Studio 用户界面元素,您的计算机可能会显示不同的名称或位置。这些元素取决于您所使用的 Visual Studio 版本和您所使用的设置。有关更多信息,请参见 Visual Studio 设置

系统必备

您需要以下组件来完成本演练:

创建网站工作流自定义活动项目

首先,创建一个用来包含和测试自定义工作流活动的项目。

创建网站工作流自定义活动项目

  1. 通过指向**“文件”菜单上的“新建”并单击“新建项目”,显示“新建项目”**对话框。

  2. 展开**“Visual C#”“Visual Basic”下的“SharePoint”节点,然后单击“2010”**。

  3. 在**“模板”窗格中选择“顺序工作流”**。

  4. 在**“名称”框中,键入 AnnouncementBackup,然后单击“确定”**。

    这将显示**“SharePoint 自定义向导”**。

  5. 在**“要使用哪个本地网站进行调试?”页中,单击“下一步”**以接受默认网站。

    此步骤还会将解决方案的信任级别设置为场解决方案(工作流项目的唯一可用选项)。

  6. 在**“指定用于调试的工作流名称”页上,接受默认名称 (AnnouncementBackup - Workflow1)。 将工作流模板类型更改为“网站工作流”,然后单击“下一步”**。

  7. 单击**“完成”**以接受剩余的默认设置。

添加自定义工作流活动类

接下来,向项目中添加一个类以包含自定义工作流活动的代码。

添加自定义工作流活动类

  1. 单击**“项目”菜单上的“添加新项”以显示“添加新项”**对话框。

  2. 在**“已安装的模板”树视图中,单击“代码”节点,然后在项目项模板列表中单击“类”**。 使用默认名称 Class1。

  3. 将 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 Namespace
    
    using 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);
            }
    
    
        }
    }
    
  4. 保存项目,然后单击**“生成”菜单上的“生成解决方案”**。

    Class1 将作为工具箱中**“SharePoint 工作流”选项卡下的“工具箱”**中的自定义操作出现。

向网站工作流中添加自定义活动

接下来,向工作流中添加一个活动以包含自定义代码。

向网站工作流中添加自定义活动

  1. 在设计视图中,在工作流设计器内打开 Workflow1。

  2. 单击 Class1 并将其从工具箱中拖放到 onWorkflowActivated1 活动下方。

  3. 保存项目。

测试网站工作流自定义活动

紧接着,运行项目并启动网站工作流。 自定义活动会创建一个备份的公告列表,然后将当前公告列表中的内容复制到该列表中。 在创建备份列表之前,代码还会检查是否已存在备份列表。 如果已存在备份列表,则会将其删除。 代码还会向 SharePoint 网站的快速启动栏上的新列表中添加链接。

测试网站工作流自定义活动

  1. 按 F5 运行项目,并将其部署到 SharePoint。

  2. 在快速启动栏上,单击**“列表”以显示 SharePoint 网站中可用的所有列表。 请注意,仅有一个名为“公告”**的公告列表。

  3. 在 SharePoint 网页顶部,单击**“网站操作”按钮,再单击“网站工作流”**。

  4. 在“启动新工作流”部分下,单击 AnnouncementBackup - Workflow1 的链接。 这将启动网站工作流,并运行自定义操作中的代码。

  5. 单击快速启动栏上显示的名为“Announcements Backup”(公告备份)的链接。 请注意,**“公告”**列表中包含的所有公告已复制到此新列表中。

请参见

任务

如何:创建事件接收器

其他资源

开发 SharePoint 解决方案