共用方式為


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

本逐步解說示範如何使用 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] 的連結。 請注意,[公告] 清單中包含的所有公告都已複製到這個新清單。

請參閱

工作

HOW TO:建立事件接收器

其他資源

開發 SharePoint 方案