Custom Workflow Activity For SharePoint Designer 2010 Using Visual Studio 2010

This article explains how to create a Custom Workflow Action in Visual Studio 2010 and how to use it in SharePoint Designer 2010 to create a workflow.

  1. Open Visual Studio 2010 and create a new SharePoint Empty project and provide the project name as WFProjects.CustomWFActivity.CustomWFAction and click OK.

    SharePoint Empty project

  2. Provide site collection name and deploy as Farm solution and click Finish.
  3. Add a reference for Microsoft.SharePoint.WorkflowActions.dll from the ISAPI folder (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI)
  4. Add a reference for System.Workflow.Activities and System.Workflow.ComponentModel from the .Net tab as below.

    Add reference

  5. Add a new class to the project and name it “CreateCustomList.cs”.
  6. Add the namespace and inherit the activity class as shown below.

    namespace

  7. Add a Dependency property to the workflow class and it will be useful to get the input from the user to do some operation. A dependency property always ends with the keyword Property otherwise a compilation error will occur.
  8. For our purposes, we need the following Dependency Property.
    a. ListName, ListDescription, ListTemplateName
  9. Write the following code in the class.

    code inside the class

  10. Add a workflow context Dependency Property to initialize the workflow object context as shown below.

    Dependency Property

  11. Next we need to override the Execute method as below.

    Execute method

  12. Write the following code to execute the method:

    code to Execute method

    dd SharePoint


  13. Right-click on the project and add the SharePoint Mapped folder. Select the workflow and click OK as shown below.

    Add XML
  14. Add a XML file into the workflow folder named CreateCustomList.actions and click the Add button.

    Add XML14

  15. Locate the site http://johnholliday.net/download/WSSACTIONS.xsd.
  16. Copy the entire code from the preceding site and save it as wssactions.xsd.
  17. Paste the wssactions.xsd file into the “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\XML” folder location.
  18. Open the CreateCustomList.actions file from the Visual Studio and press F4 to open the properties window.
  19. Select the schemas property and click on the button (…).

    schemas property

  20. Then click on the Add button from the window.

    Add button

  21. Then go to the XML folder location that we paste the wssactions.xsd file in Step 17 and select wssactions.xsd and click Open.

    wssactions.xsd

  22. Then it will be added to the window and click OK to close the window.

    added to the window

  23. Then type the “<” symbol in the actions XML file and see that the intellisence will appear to write the actions code.

    intellisence

  24. Then write the following code in the actions XML file. Please change the class name, assembly and PublicKeyToken accordingly.

    PublicKeyToken

  25. Refer to the following article for how to get PublicKeyToken using Visual Studio 2010: http://blogs.msdn.com/b/miah/archive/2008/02/19/visual-studio-tip-get-public-key-token-for-a-stong-named-assembly.aspx
  26. Next we need to make entry in the web.config file. Add a feature event receiver to the project and add the following code inside the feature receiver. Include the namespace as below before starting the code. Set the scope of feature to WebApplication.

    using Microsoft.SharePoint.Administration;

    Include the namespace

  27. Build and deploy the project.
  28. Verify that the following entry is available in the web.config file.

    below entry

  29. Open the Web Application page in Central Administration and verify the feature is activated.

    activated

  30. Open the site you want to develop the workflow in SharePoint Designer 2010.
  31. Click on Workflows.

    Click on Workflows

  32. Click List workflow and select any document or list you want to associate with the workflow, then provide the workflow name (for example Custom WF) and click OK.
  33. Then you will see our newly created Create Custom List Actions under the Actions pane.

    Create Custom List

  34. Then change the name from Step 1 to Create Custom List.

    change the name

  35. Then drag and drop the activities to the designer as shown below.

    activities to the designer

  36. Then provide the value as shown below.

    provide the value

  37. Save and publish the workflow.
  38. Go to the site and navigate to the list or Document Library you have deployed the workflow to.
  39. Upload a new document (here I deployed to a Document Library).

    new document

  40. Click on Workflows from the ribbon and click on Custom WF to start the workflow and press the start button to start the workflow.

    start the workflow

    start button


  41. Then wait a few seconds and see the WF as completed.

    WF

  42. Click on All Site Contents and see our custom Survey list created using WF.

    custom Survey