Step 3: Create the Workflow

In the first two parts of this walkthrough, you created the workflow initiation and task edit forms by using Microsoft Office InfoPath 2007. 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 2005 Designer for Windows Workflow Foundation.

Prerequisites

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

Note

As with all Microsoft Office SharePoint Server 2007 development, you should create your workflows by using a development environment that closely resembles the environment where the code will be deployed. While 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 Microsoft Office SharePoint Server 2007-specific workflow activities require that Windows SharePoint Services and Microsoft Office SharePoint Server 2007 be installed on the computer you use to develop the workflows.

To create a new Office SharePoint Server 2007 workflow project

  1. Open Visual Studio 2005.

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

  3. In the New Project window, view the Project types pane to find the development language choices.

  4. Expand the language that you will use to develop your workflow.

  5. Select the SharePoint option.

  6. In the Templates pane, select SharePoint Sequential Workflow.

  7. Name your project, and then click OK.

    Visual Studio 2005 Designer for Windows Workflow Foundation opens a new workflow project. This workflow project contains the necessary references to Windows SharePoint Services 3.0, and contains workflow activities on the toolbox that were specifically designed for Windows SharePoint Services 3.0 workflows.

  8. In order to programmatically access the XML schema that represents the forms you created in the first two procedures, add the class file that you created earlier to your project.

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

Adding and configuring workflow activities

Now that your new workflow project has been created, it is time to start designing your workflow.

In this workflow, there are a total of 5 activities.

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 Windows SharePoint Services Sequential Workflow project template, Visual Studio 2005 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 Windows SharePoint Services 3.0 workflows must start with this activity.

    Note

    If the graphical representation of the workflow is not displayed, in Solutions Explorer, double-click the Workflow1.cs file. If the workflow designer still does not have the OnWorkflowActivated activity prepopulated, ensure that you used the correct Windows SharePoint Services 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 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 and Path properties are set to workflowToken and workflowProperties, respectively. 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 they are not present, add the following using statements.

    using System.Xml;
    using System.Xml.Serialization;
    using 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);
    
  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();
    }
    
  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;
    }
    

To add a CreateTask activity

  1. From the Visual Studio 2005 Toolbox, in the Windows SharePoint Services section, drag a CreateTask activity onto the workflow design surface and drop it immediately 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 the taskToken, taskId and taskProps are variable names. The taskToken variable is a correlation token that enables Office SharePoint Server 2007 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 2005 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();
    
  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;
    }
    

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 2005 toolbox, drag a While activity onto the workflow design surface and drop it immediately 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 2005 toolbox, drag an OnTaskChanged activity on to the workflow design surface and drop it within 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 the Path property type afterProps.

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

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

    4. For the Invoked property, type onTaskChanged. This method will be 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 and TaskId properties 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 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 2005 will automatically add 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();
    
  4. Add code to the onTaskChanged method.

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

      private bool isFinished = false
      
    2. Add code to set the iisFinished 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 needs to access this custom property in the afterProps variable.

      private void onTaskChanged(object sender, ExternalDataEventArgs e)
      {
         isFinished = bool.Parse(afterProps.ExtendedProperties[“isFinished”].ToString());
      }
      
  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
    }
    

    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 2005 toolbox, drag a CompleteTask activity onto the workflow design surface and drop it immediately 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 and the TaskId Path property type taskId.

    Note

    Notice that the CorrelationToken and TaskId properties are set to the variables 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 Windows SharePoint Services Software Development Kit (SDK).

After you associate the workflow with a document library or list, you can debug your workflow by using Visual Studio 2005 Designer for Windows Workflow Foundation. For details, see How to: Debug Your Windows SharePoint Services Workflow in the Windows SharePoint Services Software Development Kit (SDK).

See Also

Concepts

Workflows in Office SharePoint Server 2007
InfoPath Forms for Workflows