Share via


Walkthrough: Import a SharePoint Designer Reusable Workflow into Visual Studio

This walkthrough demonstrates how to import a reusable workflow created in SharePoint Designer 2010 into a Visual Studio SharePoint workflow project.

Workflows created in SharePoint Designer, or declarative workflows, consist of XML statements instead of code. SharePoint Designer 2010 introduces reusable workflows, which are portable, declarative workflows that can be used by different lists in SharePoint sites.

Workflows created in Visual Studio 2010, such as sequential and state machine workflows, are called code workflows. Code workflows consist of XML files and code modules in which users can customize the workflow's behavior.

Visual Studio 2010 allows you to import reusable workflows created in SharePoint Designer 2010 and convert them to code workflows for use in your SharePoint sites.

This walkthrough demonstrates the following tasks:

  • Creating a simple, reusable workflow in SharePoint Designer.

  • Exporting the SharePoint Designer reusable workflow to a .wsp file and into SharePoint.

  • Importing the .wsp file into Visual Studio by using the Import Reusable Workflow project.

  • Altering the workflow by adding code.

  • Using the imported workflow in a SharePoint site.

Note

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.

Prerequisites

You need the following components to complete this walkthrough:

Create Target SharePoint Subsites

First you create two new SharePoint subsites: one to host the reusable workflows from SharePoint Designer, another to host the converted workflows.

To create SharePoint subsites

  1. In SharePoint Designer 2010, click Open Site on the File menu.

  2. In the Open Site dialog box, browse to a SharePoint site where you want to create the workflow, or use the default value of http://system name/ and then click Open.

    The Home page appears.

  3. In the Subsites section, click New.

  4. In the New dialog box, select SharePoint Templates in the left pane, and select Team Site in the right pane.

  5. In the Specify the location of the Web site box, replace the word subsite in the URL with SPD1 and then click OK.

    This opens the new subsite into SharePoint Designer. Close this instance of SharePoint Designer and go back to the first instance (the top-level site).

  6. Repeat steps 3 - 5 to create the second subsite, this time replacing the word subsite in the URL with SPD2.

Create a SharePoint Designer Reusable Workflow

Because SharePoint does not include any reusable workflows that you can use for this example, you will create one. In this simple workflow, when a user enters a new task in the Task list that has a specific title, the task is assigned to that user.

To create a SharePoint Designer reusable workflow

  1. Click the SPD1 site in the Subsites section to modify it.

  2. Click Reusable Workflow on the ribbon.

    The Create Reusable Workflow wizard appears.

  3. In the Name box, type SPD Task Workflow.

  4. In the Content Type box, click Task, and then click OK.

    The workflow opens in the SharePoint Designer workflow designer.

  5. Make sure that step 1 is selected and click the Condition button on the ribbon, and then select If current item field equal value from the list of conditions.

    This adds a new condition named If field equal value.

  6. Click the field link in the If field equals value condition.

  7. In the list of values, select Title.

  8. Click the value link in the If field equals value condition.

  9. In the box, enter New task.

    The condition statement now reads If Current Item:Title equals New task.

  10. Click the line under the condition statement, click the Action button on the ribbon, and then select Set field in current item from the list of actions.

  11. Click the field link in the Set field to value action, and then select Assigned to.

  12. Click the value link in the Set field to value action, select User who created the item from the list of existing users and groups, click the Add button, and then click OK.

    The action statement now reads Set Assigned To to Current Item:CreatedBy.

Save and Deploy the Reusable Workflow

Because Visual Studio can import only .wsp files, you must save the reusable workflow as a .wsp file and deploy it to SharePoint before importing it into Visual Studio.

Important

If you receive a runtime error performing the following procedure, you have to perform the procedure on a system that has access to the SharePoint site.

To save and deploy the reusable workflow

  1. Click the Save button at the top of SharePoint Designer to save your progress, and then click the Publish button to deploy the workflow to the SPD1 SharePoint site.

  2. Click Workflows in the Navigation pane.

  3. Click SPD Task Workflow under Reusable Workflow.

  4. On the ribbon, click Save as Template to save the workflow as a .wsp file.

  5. Open the SPD1 SharePoint site in a browser to view the .wsp file in SharePoint.

  6. Click Libraries on the QuickLaunch bar.

  7. Click Site Assets in the Document Libraries section.

    The SPD Task Workflow file is listed with other site assets.

  8. Click its name in the list and the File Download dialog box appears.

  9. Click the Save button in the File Download dialog box to save the .wsp file on the system.

Import the .wsp File into Visual Studio

Import the .wsp file into Visual Studio by using an Import Reusable Workflow project. This project converts the workflow from a reusable, declarative workflow into a code workflow. After the workflow is converted, you will use code to modify its behavior.

To import a workflow from a .wsp file and modify it

  1. In Visual Studio, display the New Project dialog box by pointing to New on the File menu and then clicking New Project.

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

  3. Click Import Reusable Workflow in the Templates pane, leave the name of the project as WorkflowImportProject1, and then click OK.

    The SharePoint Customization Wizard appears.

  4. On the Specify the site and security level for debugging page, enter the URL for the second SharePoint subsite that you created previously: http://system name/SPD2.

  5. In the What is the trust level for this SharePoint solution? section, leave the selection as Deploy as a farm solution.

    For more information about sandboxed versus farm solutions, see Sandboxed Solution Considerations.

  6. In the Specify the new project source page, browse to the location on the system where you previously saved the .wsp file and then click Next.

    Note

    Clicking Finish on this page causes all available items in the .wsp file to be imported.

    This displays a list of reusable workflows available for importing.

  7. In the Select items to import box, select the workflow SPD Task Workflow and then click Finish.

    After the import operation is finished, a project named WorkflowImportProject1 is created containing a workflow named SPD_Workflow_TestFT. In this folder is the workflow's definition file Elements.xml and the workflow designer file (.xoml). The designer contains two files: the rules file (.rules) and the code-behind file (either .cs or .vb, depending on your project's programming language).

  8. Set the workflow project item WorkflowImportProject1 as the Startup Item.

    This displays the list immediately when you debug the project.

  9. Because the Import Reusable Workflow does not import the association property values for the imported workflow, you must enter them. To do this:

    1. Click the SPD_Workflow_TestFT workflow project item in Solution Explorer.

    2. Click the ellipsis (ASP.NET Mobile Designer ellipse) button next to the Target List property, or any other property that has an ellipsis button.

    3. Fill out the missing values in the SharePoint Customization Wizard. When you are done, click Finish.

  10. Right-click the .xoml file and select View Designer to view the imported workflow in the workflow designer.

  11. In the Toolbox, in the Windows Workflow v3.0 section, drag a Code activity to the designer and drop it underneath the SequenceActivity1 activity.

    This adds an activity to the workflow designer named CodeActivity1. In this activity, you will add a code action that creates an announcement in the Announcements list when the user starts the workflow.

  12. Double-click CodeActivity1 to generate an event handler and view the code.

  13. Add the following under the existing using or Imports statements:

    Imports Microsoft.SharePoint
    Imports System
    
    using Microsoft.SharePoint;
    using System;
    
  14. Replace codeActivity1_ExecuteCode with the following:

    Private Sub codeActivity1_ExecuteCode(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            ' Get reference to SharePoint site.
            Dim site As SPSite = New SPSite("http://MyServer")
            Dim web As SPWeb = site.OpenWeb("SPD2/")
            ' Get reference to Announcements list.
            Dim announcementsList As SPList = web.Lists("Announcements")
            ' Add announcement to Announcements list for the Task.
            Dim oListItem As SPListItem = announcementsList.Items.Add
            oListItem("Title") = ("Assigned task on " + DateTime.Now.ToString)
            oListItem.Update()
        Catch err As Exception
            Console.WriteLine(("Error: " + err.ToString))
        End Try
    End Sub
    
    private void codeActivity1_ExecuteCode(object sender, System.EventArgs e)
    {
    
        try
        {
            // Get reference to SharePoint site.
            SPSite site = new SPSite("http://MyServer");
            SPWeb web = site.OpenWeb("SPD2/");
            // Get reference to Announcements list.
            SPList announcementsList = web.Lists["Announcements"];
    
            // Add announcement to Announcements list for the Task.
            SPListItem oListItem = announcementsList.Items.Add();
            oListItem["Title"] = "Assigned task on " + DateTime.Now.ToString();
            oListItem.Update();
        }
    
        catch (Exception err)
        {
            Console.WriteLine("Error: " + err.ToString());
        }        
    }
    

Deploy the Project and Associate the Workflow

Next, run WorkflowImportProject1 to deploy it to a SharePoint site and then associate the workflow with the Tasks list to view and test the modified, converted workflow.

To deploy the project and associate the workflow

  1. In Visual Studio, press F5 to run and deploy the converted workflow project.

  2. Click Tasks on the QuickLaunch bar to display the Tasks list.

  3. Click Items on the List Tools ribbon, and then click the New Item button.

  4. In the Tasks - New Item dialog box, enter New task in the Title box and then click Save.

  5. Display the List Settings page by clicking List on the List Tools ribbon button, and then clicking List Settings.

  6. Click the Workflow Settings link in the Permissions and Management section.

    The Add a Workflow page appears.

  7. In the Workflow list, select WorkflowImportProject1 - SPD Workflow Test.

  8. In the Name box, enter SPD Workflow Test and then click OK.

  9. Click Tasks on the QuickLaunch bar.

  10. Click the drop-down arrow next to New task and select Workflows.

  11. Click the link for SPD Workflow Test in the Start a New Workflow section, and then click Start to initiate the workflow.

    Note

    Alternatively, you can auto-associate a workflow with a list by running the workflow settings wizard and setting the workflow to auto-associate.

    Notice that two actions are performed by the workflow: your name appears in the task's Assigned To column, and an announcement appears in the Announcements list.

See Also

Concepts

Importing Items from an Existing SharePoint Site

Other Resources

Developing SharePoint Solutions

Creating Reusable Controls for Web Parts or Application Pages