Walkthrough: Create a Custom Site Workflow Activity

This walkthrough demonstrates how to create a custom activity for a site-level workflow using Visual Studio. (Site-level workflows apply to the whole site, not just a list on the site.) The custom activity creates a backup Announcements list and then copies the contents of the Announcements list into it.

This walkthrough demonstrates the following tasks:

  • Creating a site-level workflow.

  • Creating a custom workflow activity.

  • Creating and deleting a SharePoint list.

  • Copying items from one list to another.

  • Displaying a list on the QuickLaunch bar.

NoteNote

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

You need the following components to complete this walkthrough:

First, create a project to hold and test the custom workflow activity.

To create a site workflow custom activity project

  1. Display the New Project dialog box by pointing to New on the File menu, and then click New Project.

  2. Expand the SharePoint node under either Visual C# or Visual Basic, and then click 2010.

  3. In the Templates pane, select Sequential Workflow.

  4. In the Name box, type AnnouncementBackup and then click OK.

    The SharePoint Customization Wizard appears.

  5. In the What is the local site you want to use for debugging? page, click Next to accept the default site.

    This step also sets the trust level for the solution as farm solution, the only available option for workflow projects.

  6. In the Specify the workflow name for debugging page, accept the default name (AnnouncementBackup - Workflow1). Change the workflow template type to Site Workflow and then click Next.

  7. Click Finish to accept the remaining default settings.

Next, add a class to the project to contain the code for the custom workflow activity.

To add a custom workflow activity class

  1. Click Add New Item on the Project menu to display the Add New Item dialog box.

  2. In the Installed Templates tree view, click the Code node and then click Class in the list of project item templates. Use the default name Class1.

  3. Replace all of the code in Class1 with the following:

    
    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
    
    
    
  4. Save the project and then click Build Solution on the Build menu.

    Class1 appears as a custom action in the Toolbox under the SharePoint Workflow tab in the Toolbox.

Next, add an activity to the Workflow to contain the custom code.

To add a custom activity to the site Workflow

  1. Open Workflow1 in the workflow designer in design view.

  2. Click and drag Class1 from the Toolbox and drop it under the onWorkflowActivated1 activity.

  3. Save the project.

Next, run the project and start the site workflow. The custom activity creates a backup Announcements list and copies the contents from the current Announcements list into it. The code also checks whether a backup list already exists before creating one. If a backup list already exists, it is deleted. The code also adds a link to the new list on the SharePoint site's QuickLaunch bar.

To test the site workflow custom activity

  1. Press F5 to run the project and deploy it to SharePoint.

  2. On the QuickLaunch bar, click Lists to display all of the lists available in the SharePoint site. Notice there is only one list for announcements named Announcements.

  3. At the top of the SharePoint Web page, click the Site Actions button and then click Site Workflows.

  4. Under the Start a New Workflow section, click the link for AnnouncementBackup - Workflow1. This starts the site workflow and runs the code in the custom action.

  5. Click the link called Announcements Backup that appears on the QuickLaunch bar. Notice that all of the announcements that are contained in the Announcements list have been copied to this new list.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft