Ingo Karstein's Blog

SharePoint & PowerShell

Walkthrough: Creating a simple Sequential Workflow with a custom Task Form in SharePoint 2010 using Visual Studio 2010 (Part 2 of 2)

Posted by Ingo Karstein on 2011/03/30

In this walkthrough I want to show you how to create a Sequential Workflow with Visual Studio 2010 for use in SharePoint 2010. – I will show how to create a custom Task Form for interaction with users. The Task form will be a native SharePoint list form. No InfoPath. There are many InfoPath samples out there but they cannot be used on a SharePoint Foundation 2010 platform. But workflows can be used on SharePoint Foundation 2010 too!

To reproduce the following steps you need to create a SharePoint site. – In the walkthrough I’ll use a Site Collection located at “http://sharepoint.local/sites/workflow”.

This blog post is part 2 of 2. It describes the steps 16 to 27. Read part 1 here:

You can download – and help to develop – the whole demo project source code at Codeplex:

(If you need assistance for the deployment process because of deployment errors please see this third post of this series:

Let’s start (again)…

16. Now we need to add a list that we use to connect a workflow with. We create a “List Definition” project item named “Workflow 1 Host List”.



We change the “Elements.xml” file of the List Instance project item.


We change the Title, URL and Description.


17. Deploy the project.

18. Now we start creating a Sequential Workflow.

Create a “Sequential Workflow” project item named “Workflow 1”.


Create the workflow as “List Workflow”.


You will see all the lists we created in the previous steps. Remember: You have to (successfully) deploy the project before you can use the lists that will be created during deployment. – Without previous deployment the wizard may fail before you see the following dialog page.

In the first dropdown list choose “Workflow 1 Host List”. All other lists should be selected automatically because there is only one usable list of every needed type.


Select all “Workflow start” options: “Manually”, “Item Created”, “Item Updated”.


You get:


19. We will add a “Create Task With Content Type” workflow activity that will use our “Workflow 1 Task 1” we created in the steps 1 to 15 (blog post “part 1”).

Open the “Toolbox” pane and drag “CreateTaskWithContentType” into the Workflow Designer.


After you did this you get:


Now we need to configure the workflow activity in the “Properties” pane.

Set “correlationToken” to “task1” and the sub element of “correlationToken” named “OwnerActivityName” to “Workflow_1”.


Now we need to create code behind properties for some activity properties. As example I’ll show how to create a code behind property for “ContentTypeId”.

Click on the Button “…” at the activity property edit box:


In the dialog select the tab “Bind to a new member”. Enter the name “New member name” and select the “Create Property” radio button. Press “OK”.


Here are the property bindings you need to create:

  • “ContentTypeId” = “task1ContentTypeId”
  • “TaskId” = “task1Guid”
  • “TaskProperties” = “task1Properties”

In “MethodInvoking” type “createTask1Invoke” and press ENTER. – You’ll be directed to the code editor.

(The activity properties look like this after you finished: image)

In the method we initialize the task1-Properties we created before. The “ContentTypeId” is taken from the “Schema.xml” file of our “Workflow 1 Tasks” list where we created the Content Type “Workflow 1 Task 1”.

After that your method looks like this:


20. Back in the workflow designer. We drag a “While” activity from the Toolbox pane into the workflow behind the “Create Task” activity.


Into the “While” activity we drag a “Sequence” property from the Toolbox pane. We get:


Into the “Sequence” activity we drag a “OnTaskChanged” activity from the Toolbox pane. We get:


Now we edit the properties of the created “OnTaskChanged” activity. We have to set the “CorrelationToken” as described above. Additionally we specify new property bindings:

  • “AfterProperties” = “task1Changed1_AfterProperties”
  • “BeforeProperties” = “task1Changed1_BeforeProperties”
  • “TaskId” => Bind to the existing member “task1Guid”!!!

In the “Invoked” property add the method name “task1Changed1Invoke”. – The code editor will be opened.

(The activity properties look like this after you finished: image)

21. In the code we add a property at class level:


This property we set inside the “OnWorkflowItemChanged” activity. It will contain the information whether the Workflow Task Item we created before was “Completed” by the assigned user.

In the “task1Changed1Invoke” method we set the “isFinished” property.


22. Now we need to specify the condition for the “While” activity. In the workflow designer select this activity. In the “Condition” property select “Code Condition” and expand the property. Enter “while1Invoke” in the subproperty with (the same) name “Condition”.


In the code editor enter the code for “while1Invoke” like this:


The “e.Result” property has to be “TRUE” as long as the while loop should run. It should not run anymore (“e.Result = false”) if “isFinished” is TRUE.

23. In the workflow designer add a “CompleteTask” activity behind the “While” activity. Drag the “CompleteTask” activity from the Toolbar pane into the workflow designer. We get:


Select the “completeTask1” activity and edit it’s properties in the Property pane.

  • Bind “TaskId” to the existing member “task1Guid”
  • Create a new member namend “task1Outcome” for binding to “TaskOutcome”
  • Set the “CorrelationToken” to “task1”

24. Whew! Smile

25. Deploy the project. – It deployed successfully ?! – Congratulations!

26. Open the browser and navigate to “http://sharepoint.local/sites/workflow”. Open the “Workflow 1 Host List”. Add a new item… – The “Workflow 1” should start automatically.



(After F5 / page refresh:)


Click “In Process”. We see the Workflow Status page:


In the “Tasks” section open the context menu of the element “(no title)”. – We did not set a title for the created “Task 1”. This could be done in the method “createTask1Invoke”.


Click “Edit Item”.

There it is!!! :


Just edit a property and click “Save As Draft”.

On the Workflow Status page you see that the workflow is still running but was modified (“Last run”).


Then edit the task again. Click “Complete Task”.

After that the workflow is “Completed”:


27. We have done it! – Now we could add a “Task 2” for this “Workflow 1” where some other data is requested from users.

With that method you will be able to add custom forms for Workflow Tasks. You could also use forms you create with Silverlight !!!

Thank you for your kind attention!

31 Responses to “Walkthrough: Creating a simple Sequential Workflow with a custom Task Form in SharePoint 2010 using Visual Studio 2010 (Part 2 of 2)”

  1. Ip zufa said

    hi ikarstein,

    your demo is great. however, there is a bug with ‘Workflow 1 Tasks’, if you add a new item in this list by ‘Add new item’, then you modify it. the first time of modifiction is all right, but the second time of modification, you will get error “This task is currently locked by a running workflow and cannot be edited. “. the reason is the currentitem[WorkflowVersion] = 512. then the system think it has been locked. but if you do ‘currentitem[WorkflowVersion] = 1’, then you can update it. but , the item won’t be fired onTaskchanged event. it only happended for those creating by ‘Add new item’. the item created by workflow is working fine. but, i haven’t figure out how to fix it.

  2. kashek85 said

    Reblogged this on My Sharepoint Experiences.

  3. mkk said

    Thanks Karstein for detailed blog post.

    I completely followed blog post and able to successfully create the workflow, now i am in the process of changing the workflow to approval/reject and added association and initiation pages as well.

    My question is when the workflow in rejected it need to go back to initiator to re-submit (after making necessary changes) and then it should goto approver again. What is the way/best way to do this, can you provide any sample for this or any ideas.

  4. I do trust all the ideas you’ve introduced on your post. They’re very convincing and can certainly work. Nonetheless, the posts are very brief for novices. Could you please lengthen them a bit from next time? Thank you for the post.

  5. Pooja said


    Thanks for the great article. But I have one problem, I want to use an infopath basd form as Template type for the Host List being created. Can you give me some clue on how should I go about acheiving that?

  6. mobila bistrita…

    […]Walkthrough: Creating a simple Sequential Workflow with a custom Task Form in SharePoint 2010 using Visual Studio 2010 (Part 2 of 2) « Ingo Karstein's Blog[…]…

  7. Luca Menegozzo said

    Great post !!

  8. Tom said

    Hi again Ingo

    Part 2 was a lot easier than Part 1!! I had to remove the kludge which I added for testing at the end of Part 1 (the line li[SPBuiltInFieldId.WorkflowVersion] = 1; ). Once the content type was connected to the workflow, it prevented the OnTaskChanged item from firing, but the problem with ‘task locked by running workflow’ went away at the same point. Thanks again for your blog.

  9. Alexander said

    Hi Ingo!
    One more THANK YOU VERY MUCH for this guide to your Thanks-Box
    But this guide, like all the others, does not let the assigned approver to say, that document (list item, whatever) is not ready (=not good enought). How do you usually work this out? What task status do you use? How do you inform the wf_initiator that some more work should be done?

  10. I want create a solution or package to install on the production environment, how can I do this?? “please in details”

  11. Clint said

    Thanks for the post!

    I have finished your walk through, and I’m trying to expand on it. Mainly adding different controls (e.g. RadioButtonList, DropDown, CheckBox, etc.), and how to reference them in the workflow code-behind.

  12. Rohit said

    I am able to deploy both the projects, but on adding an item in the “workflow 1 host list” the workflow status remains “Failed on Start (retrying)”. I followed all the steps twice, but I get this error still.

    What might be the reason?



  13. divya said

    Hi ,
    I tried with the adding EditTask Form.aspx page in BannerElement as you suggested, and add Module Tag in it’s Element.xml
    But after deployment I am getting Page Cannot found Error , Will you please help me i added the Module in my Feature where Workflow Feature is added, will you please help It’s urgent

    Thanks in Advance

    • ikarstein said

      Please check the 14-hive in the file system and search the page in the site structure with SharePoint Designer 2010. – Can you find the page? Can you access them by specifying the page url in the browser directly? I’d think that your module project item is wrong configured!

  14. Saiprasad said

    Hi Ingo,

    First of all thank you for this aritcle. I Sucessfully implemented this in my project.

    Custom edit task form works fine with inbulit sharepoint 2010 master pages. But after changing inbulit master page to custom master page, custom task form not works. It thorws below error

    The attribute ‘autoeventwireup’ is not allowed in this page.

    Troubleshoot issues with Microsoft SharePoint Foundation.

    Correlation ID: 50c76a6d-4926-41d7-82be-858b70e48938

    Date and Time: 8/4/2011 2:41:58 PM

    Please help me out to solve this problem.


    • ikarstein said

      just remove the attribute and register the type as “safetype” in visual studio. – did this solve your problem??

      • Saiprasad said

        Hi Ingo,

        Can you please guide me to registerthe type as “safetype” for my custom task form.


        • ikarstein said


          1. Copy the Namespace of your Custom Form code behind class to the clipboard.
          2. Go into Solution Explorer.
          3. Create an “Empty Element” Project item.
          4. Add this new project item to your SharePoint Feature (in the VS Solution)
          5. Select the “Empty Element” project item in the solution explorer.
          6. Click “SafeTypes” in the Properties pane.
          7. Add a new “SafeType”. Enter the copied namespace in the SafeTypes properties.

          Than try it.


  15. Tony Barnett said

    Hi Ingo

    These may be rudimentary questions, but I just can’t seem to find any answers to the following:

    Who do you check, in Visual Studio 2010 in a Sequential Workflow, if the workflow was started when an item is created or when it has been modified? The reason being is I need to preform different workflow actions depending on if it is created or not? I have tried to IfElse activity but I’m not sure what to check for.

    Also is it possible to have two different workflows, the first workflow starts on item create, which contains a CreateTask action and the second workflows, which is starts on item modified, which has an UpdateTask which sets properties on the same task which was created in the first workflow. The reason being is I receive an error when I run the second workflow complaining about CorrelationToken. How can I use the same / reference the same CorrelationToken for the two Tasks?


    • ikarstein said

      Hi Tony!

      Your first question… In the Visual Studio solution you can configure the workflow startup type by using the Properties pane of the “Workflow” project item. See this screenshot:


      Your second question: You can have multiple workflows on a List or Content Type or… All associated workflows will start as configured. If the are more than one the will run parallel and without any notice of the other. (A “connection” between them could be done in code of course.) You cannot connect them by using CorrelationTokens. Normally a workflow task item is connected to a special workflow instance. If you need to do this you have to store the workflow tasks ID at a diffrent place where both workflow instances can access this information.


  16. Luca Giornetti said


    I successfully used you tutorial thank you very much.

    I would write (using you tutorial as base) a simple document approval WF:
    The WF initiator fills a form with x approvers (person or group fields). A task is created for approver1, when approver1 approves the document, a task is created for approver2 and so on until the last approver approves his task. (if only one approver does not approve the task, the workflow ends).

    How could I do the form for the initiator? Maybe using an initiator form from VS2010? If so, how can I put in it person or groups controls?
    How could I make the approver approves his task? By clicking on complete button?

    Thank you very much for your help,

    Luca Giornetti

  17. Kgee said


    Successfully Used you guidance thank you very much.
    One question – how can i get the Custom Field value created in the ASPX and use it in the workflow.
    I created a Custom Field Called – Revision Date(DateTime Fiels) on the Task form when the date is changed i want to Update the Documeny Library item with the date value selected.


  18. Tarjei said


    Tried this tutorial. Part 1 worked fine, but part 2 gives me the ‘System.InvalidOperationException: The event receiver context for Workflow is invalid’-exception when starting the workflow. All the answers I get from Google is that either the TaskID, CorrelationToken and/or the TaskProperties are incorrect configured. I’ve checked the code and the workflow and can’t find anything wrong with how the TaskID, CorrelationToken and/or the TaskProperties are configured. Any idea would be appreciated.

    However there are a discrepancy between what you write in step 20 and the corresponding image of the properties of the onTaskChanged1-event. You write that we should use an onTaskChanged-event in the workflow, but in the image it is clearly an onWorkFlowItemChanged-event that are displayed (

    • ikarstein said


      Thanks for you comment! – You are right: There is (was) an discrepancy between the screenshot and the text. I’ve corrected the screenshot. – Did you download the complete demo project from codeplex. I tested it now again and it works on my machine.


      • Tarjei said

        I’ve now downloaded the demo project and that worked fine. A closer look revealed that it actually was something wrong with my correlationtoken-configuration. I configured the correlationtoken for the task as the same correlationtoken for the workflow, so my mistake.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 58 other followers

%d bloggers like this: