逐步解說:建立自訂站台工作流程活動

本逐步解說示範如何使用 Visual Studio 為網站層級工作流程建立自訂活動 (網站層級工作流程適用於整個網站,而不只是網站上的清單)。此自訂活動會建立備份「公告」清單,然後將「公告」清單的內容複製到該清單。

本逐步解說將示範下列工作:

  • 建立網站層級工作流程。

  • 建立自訂工作流程活動。

  • 建立和刪除 SharePoint 清單。

  • 將項目從一個清單複製到另一個清單。

  • 在快速啟動列上顯示清單。

注意事項注意事項

您的電腦可能會在下列說明中,以不同名稱或位置顯示某些 Visual Studio 使用者介面項目。您所擁有的 Visual Studio 版本以及使用的設定會決定這些項目。如需詳細資訊,請參閱 Visual Studio 設定

必要條件

您需要下列元件才能完成此逐步解說:

建立網站工作流程自訂活動專案

首先,建立專案來存放和測試自訂工作流程活動。

若要建立網站工作流程自訂活動專案

  1. 在功能表列上,選取 [檔案], [新增], [專案] 顯示 [新增專案] 對話方塊。

  2. 展開節點。 [SharePoint] [Visual C#] 或 [Visual Basic] 下的,然後選取 [2010 年] 節點。

  3. 在 [樣板] 窗格中,選取 [SharePoint 2010 專案] 範本。

  4. 在 [名稱]方塊中,輸入 AnnouncementBackup,然後選取 [確定] 按鈕。

    [SharePoint 自訂精靈] 隨即出現。

  5. 在 [指定網站和安全性層級進行偵錯] 頁面上,選取 [部署為陣列方案] 選項按鈕,然後選取 [完成] 按鈕容錯信任層級和預設網站。

    此步驟會將方案的信任層級設定為陣列方案,這是工作流程專案唯一可用的選項。

  6. 在 [方案總管],請選取中的專案節點,然後,在功能表列上,選擇 [專案], [加入新項目]。

  7. 在 [Visual C#] 或 [Visual Basic] 底下,展開[SharePoint] 節點,然後選取[2010 年] 節點。

  8. 在 [樣板] 窗格中,選取 [循序工作流程" (僅限陣列方案)] 範本,然後選取 [加入] 按鈕。

    [SharePoint 自訂精靈] 隨即出現。

  9. 在 [指定工作流程名稱進行偵錯] 頁面中,接受預設名稱 (AnnouncementBackup - Workflow1)。變更工作流程範本類型加入至 [網站工作流程],然後選取 [下一個] 按鈕。

  10. 選取 [完成] 按鈕接受其餘的預設值。

加入自訂工作流程活動類別

接下來,將類別加入至專案,以包含自訂工作流程活動的程式碼。

若要加入自訂工作流程活動類別

  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 會以 [工具箱] 的自訂動作在 [AnnouncementBackup 元件] 索引標籤。

將自訂活動加入至網站工作流程

接下來,將活動加入至工作流程,以包含自訂程式碼。

若要將自訂活動加入至網站工作流程

  1. 在工作流程設計工具的設計檢視中開啟 Workflow1。

  2. 從拖曳 [工具箱] 的 Class1,使它在 onWorkflowActivated1 活動下,或開啟 Class1 的捷徑功能表上,選取 [複製],開啟線條的捷徑功能表。 onWorkflowActivated1 活動下的,然後選取 [貼上]。

  3. 儲存專案。

測試網站工作流程的自訂活動

接下來,執行專案並啟動網站工作流程。此自訂活動會建立備份「公告」清單,然後將目前「公告」清單的內容複製到該清單。程式碼也會在建立備份清單之前檢查它是否已存在。如果備份清單已存在,則會被刪除。程式碼也會將新清單的連結加入至 SharePoint 網站的快速啟動列。

若要測試網站工作流程的自訂活動

  1. 選取 F5 鍵執行專案並將它部署至 SharePoint。

  2. 在快速啟動列上,選取 [清單] 連結顯示可在 SharePoint 網站的所有清單。請注意,公告只有一個清單,名為 [公告]。

  3. 在 SharePoint 網頁的頂端,選取 [網站工作流程] 連結。

  4. 在開始下一個新的工作流程區段中,選取 [AnnouncementBackup – Workflow1] 連結。這會啟動網站工作流程,並執行自訂動作中的程式碼。

  5. 在快速啟動列上,選取 [備份公告] 連結。請注意,[公告] 清單中包含的所有公告都已複製到這個新清單。

請參閱

工作

HOW TO:建立事件接收器

其他資源

開發 SharePoint 方案