Step 3: Create the Workflow in SharePoint Server 2010 (ECM)

Applies to: SharePoint Server 2010

In the first two parts of this walkthrough, Step 1: Create the Workflow Initiation Form in SharePoint Server 2010 (ECM) and Step 2: Create the Workflow Task Edit Form in SharePoint Server 2010 (ECM), you created the workflow initiation and task edit forms by using Microsoft InfoPath 2010. In the last step of this walkthrough, you will create a sequential workflow project and the code components of the workflow by using Visual Studio 2010 Workflow Designer.

Prerequisites

To complete this procedure, you must have completed the procedures in the following topics:

Note

As with all Microsoft SharePoint Server 2010 development, you should create your workflows by using a development environment that closely resembles the environment where the code will be deployed. Although it is not always possible to completely replicate a production environment by using developer resources, making sure that the two environments are as similar as possible will greatly simplify your development and debugging processes. For example, the SharePoint Server 2010-specific workflow activities require that SharePoint Foundation and SharePoint Server 2010 be installed on the computer that you use to develop the workflows.

To create a new SharePoint Server 2010 workflow project

  1. Open Visual Studio.

  2. On the File menu, select New, and then click Project.

  3. In the New Project dialog box, under Installed Templates, select the SharePoint 2010 templates.

  4. Select the Sequential Workflow template.

  5. Name your project, and then click OK.

    The SharePoint Customization Wizard starts.

  6. Specify this workflow as one you can deploy as a farm solution. Click Next

  7. Specify the name of your workflow and whether it will be scoped at the list or site level. For this walkthrough, you will create a list workflow.

    Note

    A list-scoped workflow can only be used for a specified list or library, but a site-scoped workflow is available to an entire site collection.

  8. To programmatically access the XML schema that represents the forms that you created in the first two parts of this walkthrough, add the class file that you created in Step 1: Create the Workflow Initiation Form in SharePoint Server 2010 (ECM).

    In Solution Explorer, right-click the project name, point to Add, and then select Existing Item. In the Add Existing Item dialog box, browse to the file location where you created your InitiationForm.cs file or InitiationForm.vb file, and click Add.

Adding and Configuring Workflow Activities

Now that your new workflow project has been created, and you have added the form schema reference, it is time to start designing your workflow.

In this workflow, there are a total of five activities, as shown in the following table.

Activity

Description

OnWorkflowActivated

Executes when the workflow is activated.

CreateTask

Creates a workflow task and assigns it to a user.

While

Executes the activities that it contains until a specific condition is no longer true.

OnTaskChanged

Executes when a workflow task is modified.

CompleteTask

Sets the workflow task as complete.

To set the properties of the OnWorkflowActivation activity

  1. Set the Invoked property of the OnWorkflowActivated activity.

    When you created your workflow project by using the SharePoint Foundation Sequential Workflow project template, Visual Studio automatically added the first activity to your design surface and created a method in the code-behind file. This first activity is called OnWorkflowActivated and has a default name of onWorkflowActivated1. All SharePoint Foundation 2010 workflows must start with this activity.

    Note

    If the graphical representation of the workflow is not displayed, in Solution Explorer, double-click the Workflow1.cs file. If the Workflow Designer still does not have the OnWorkflowActivated activity prepopulated, ensure that you are using the correct SharePoint Foundation Sequential Workflow template.

    1. In the Workflow Designer window, select the onWorkflowActivated1 activity.

    2. In the Properties window, for the Invoked property, type onWorkflowActivated, and then press Enter. Visual Studio will open the code-behind file and create the OnWorkflowActivated method if it does not already exist.

    Note

    Notice in the properties window that the CorrelationToken property is set to workflowToken and the Path property is set to workflowProperties. These are workflow variables. They allow the workflow engine to route data to the appropriate workflow instance. The workflowProperties variable object is initialized when the workflow instance is activated. This includes properties that are common to all workflows, such as the workflow instance identifier (ID) and the list item that the workflow instance is running on. It can also include custom properties passed to a custom workflow initiation form. In this case, the workflowProperties variable contains the initiation properties of the workflow instance.

  2. Ensure that the code file contains the correct references. If the references are not present, add the following using statements.

    using System.Xml;
    using System.Xml.Serialization;
    using Microsoft.SharePoint.Workflow
    
    Imports System.Xml
    Imports System.Xml.Serialization
    Imports Microsoft.SharePoint.Workflow
    
  3. Add the following string variable declarations to the Workflow1 class.

    private String assignee = default(String);
    private String instructions = default(String);
    private String comments = default(String);
    
    Private assignee As String = Nothing
    Private instructions As String = Nothing
    Private comments As String = Nothing
    
  4. Add the following code to the Workflow1 class file.

    namespace WorkflowLibrary1
    {
       public sealed partial class Workflow1: SharePointSequentialWorkflowActivity
       {
          public Workflow1()
          {
             InitializeComponent();
          }
    
       public Guid workflowID = default(System.Guid);
          public Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties 
            workflowProps = new 
            Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties();
    }
    
    Namespace WorkflowLibrary1
       Public NotInheritable Partial Class Workflow1
          Inherits SharePointSequentialWorkflowActivity
         Public Sub New()
           InitializeComponent()
         End Sub
       Public workflowID As Guid = Nothing
         Public workflowProps As New Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties()
       End Class
    End Namespace
    
  5. Add the following code to the onWorkflowActivated method.

    private void onWorkflowActivated(object sender, ExternalDataEventArgs e)
    {
       workflowID = workflowProps.WorkflowId;
       XmlSerializer serializer = new XmlSerializer(typeof(InitForm));
       XmlTextReader reader = new XmlTextReader (new System.IO.StringReader(workflowProps.InitiationData));
       InitForm initform = (InitForm) serializer.Deserialize(reader);
    
       assignee = initform.assignee;
       instructions = initform.instructions;
       comments = initform.comments;
    }
    
    Private Sub onWorkflowActivated(ByVal sender As Object, ByVal e As ExternalDataEventArgs)
       workflowID = workflowProps.WorkflowId
      Dim serializer As New XmlSerializer(GetType(InitForm))
      Dim reader As New XmlTextReader(New System.IO.StringReader(workflowProps.InitiationData))
      Dim initform As InitForm = CType(serializer.Deserialize(reader), InitForm)
       assignee = initform.assignee
     instructions = initform.instructions
      comments = initform.comments
    End Sub
    

To add a CreateTask activity

  1. From the Visual Studio toolbox, in the SharePoint Foundation section, drag a CreateTask activity onto the workflow design surface and add it directly under the onWorkflowActivated1 activity.

  2. Set the CreateTask activity properties.

    1. With the CreateTask activity selected, view the Properties window.

    2. For the CorrelationToken property, type taskToken.

    3. For the MethodInvoking property, type createTask.

    Note

    Notice that taskToken, taskId, and taskProps are variable names. The taskToken variable is a correlation token that enables SharePoint Server 2010 to route data to the appropriate task within the workflow instance. The taskId variable is a GUID that identifies the task within the workflow instance. The taskProps variable contains the properties that are used to initialize the task.

  3. Ensure that your code file has the following declarations. Visual Studio should create these automatically, but add them if they are not there.

    public Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties afterProps = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
    public Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties beforeProps = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
    
    Public afterProps As New Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties()
    Public beforeProps As New Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties()
    
  4. Add the following code to the code-behind page.

    private void createTask(object sender, EventArgs e)
    {
       taskID = Guid.NewGuid();
       taskProps.Title = "Demo Task";
       taskProps.AssignedTo = assignee;
       taskProps.Description = instructions;
       taskProps.ExtendedProperties["comments"] = comments;
       taskProps.ExtendedProperties["instructions"] = instructions;
    }
    
    Private Sub createTask(ByVal sender As Object, ByVal e As EventArgs)
       taskID = Guid.NewGuid()
       taskProps.Title = "Demo Task"
       taskProps.AssignedTo = assignee
       taskProps.Description = instructions
       taskProps.ExtendedProperties("comments") = comments
       taskProps.ExtendedProperties("instructions") = instructions
    End Sub
    

At this point in the workflow process, the task has been created and assigned to a user. Now you must add activities that enable the workflow to wait for the task to be completed by that user. You do this by using activities that represent logic flow controls for your workflow.

To add a While Activity

  1. From the Visual Studio toolbox, drag a While activity onto the workflow design surface and add it directly under the createTask1 activity.

    The While activity causes the activities inside it to loop, as long as the condition it evaluates resolves to true. In this example, you will use it to loop around the task edit event until the user explicitly completes the task.

  2. Set the properties for the While activity.

    1. Set the Condition property to Code Condition.

      When you set this property to Code Condition, it indicates to the workflow that a custom function has been created and should be used to process the while1 activity.

      When you set the Condition subproperty to notFinished, this specifies the method that should run. The method must return a Boolean value.

To add an OnTaskChanged activity

  1. From the Visual Studio toolbox, drag an OnTaskChanged activity onto the workflow design surface and add it inside the while1 activity loop.

  2. Set the properties for the onTaskChanged1 activity.

    1. Expand the AfterProperties property collection. For the Name property, type Workflow1, and for the Path property, type afterProps.

    2. Expand the BeforeProperties property collection. For the Name property, type Workflow1, and for the Path property, type beforeProps.

    3. For the CorrelationToken property, type taskToken, and for the CorrelationTokenPath property, type Workflow1.

    4. For the Invoked property, type onTaskChanged. This method is called when the onTaskChanged1activity is executed.

    5. Expand the TaskId property collection. For the Name property, type Workflow1, and for the Path property, type taskId.

      Note

      Notice that the CorrleationToken property and the TaskId property are set to the variables used in the createTask1 activity. This setting binds this activity to the same task that was created by the createTask1 activity, and it ensures that the workflow is receiving the change event for the correct task.

      Also notice that afterProps and beforeProps are object variables. The afterProps variable represents the task properties after the task change event has occurred, while beforeProps represents the task properties before the task change event occurred.

  3. Visual Studio automatically adds the appropriate variable declarations to the workflow code. However, if they are not created automatically, add the following code to the class.

    public Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties afterProps = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
    public Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties beforeProps = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
    
    Public afterProps As New Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties()
    Public beforeProps As New Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties()
    
  4. Add code to the onTaskChanged method.

    1. Add the following variable declaration to the workflow partial class.

      private bool isFinished = false
      
      Private isFinished As Boolean = False
      
    2. Add code to set the isFinished variable. This information is passed to the workflow from the task edit form.

      Note

      Remember that the task edit form contains a check box named isFinished. The check box value is a Boolean value that is stored as a custom property in the afterProps variable. The onTaskChanged method must access this custom property in the afterProps variable.

      private void onTaskChanged(object sender, ExternalDataEventArgs e)
      {
         isFinished = bool.Parse(afterProps.ExtendedProperties["isFinished"].ToString());
      }
      
      Private Sub onTaskChanged(ByVal sender As Object, ByVal e As ExternalDataEventArgs)
         isFinished = Boolean.Parse(afterProps.ExtendedProperties("isFinished").ToString())
      End Sub
      
  5. Add code to the notFinished method.

    Each time the task is changed, the while1 activity invokes this method to determine whether its condition is met. As long as the Result property of the ConditionalEventArgs object evaluates to true, the while1 activity will continue to wait.

    Add code that sets the Result property of the ConditionalEventArgs object.

    private void notFinished(object sender, ConditionalEventArgs e)
    {
       e.Result
    }
    
    Private Sub notFinished(ByVal sender As Object, ByVal e As ConditionalEventArgs)
       e.Result
    End Sub
    

    Now, each time the user edits the task, the onTaskChanged1 activity handles the task changed event. It invokes the onTaskChanged method, which examines the task properties and sets the isFinished variable to represent whether the user marked the task as complete. The while1 activity then invokes the notFinished method, which sets the result of the event to the opposite of the isFinished variable. If isFinished returns false, the event result is set to true, and the while1 activity keeps waiting for task changes; if isFinished is equal to true, the event result is set to false, and the while1 activity completes, and the workflow continues to the next activity.

To add a CompleteTask activity

  1. From the Visual Studio toolbox, drag a CompleteTask activity onto the workflow design surface and add it directly under the while1 activity.

  2. Set the properties for the CompleteTask activity.

    1. For the CorrelationToken property, type taskToken.

    2. For the CorrelationToken Path property, type Workflow1.

    3. For the TaskId Name property type Workflow1.

    4. For the the TaskId Path property type taskId.

    Note

    Notice that the CorrelationToken property and the TaskId property are set to the variables that are used in the createTask1 activity. This binds this activity to the same task that was created by the createTask1 activity.

Now that your workflow is complete, you are ready to test, debug, and deploy your workflow and the forms that accompany it.

Next Steps

To make the workflow available for association with document libraries in Office SharePoint Server 2007, you must still compile the workflow assembly, install the workflow as a Feature, and activate the workflow Feature on the selected site.

For information about performing these next steps, see How to: Deploy a Workflow Template in the SharePoint Foundation Software Development Kit (SDK).

After you associate the workflow with a document library or list, you can debug your workflow by using Visual Studio 2010 Workflow Designer. For more information, see How to: Debug Your SharePoint Foundation Workflow in the SharePoint Foundation SDK.

See Also

Tasks

Walkthrough for SharePoint Server 2010 (ECM): Creating SharePoint 2010 Workflows in Visual Studio Using InfoPath Forms

Concepts

Workflows in SharePoint Server 2010 (ECM)

InfoPath Forms for Workflows (ECM)