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 1 of 2. It describes the steps 1 to 15. Read part 2 here: http://ikarstein.wordpress.com/2011/03/30/walkthrough-creating-a-simple-sequential-workflow-with-a-custom-task-form-in-sharepoint-2010-using-visual-studio-2010-part-2-of-2/
You can download – and help to develop – the whole demo project source code at Codeplex: http://spworkflowdemo.codeplex.com/
1. We create the Site Collection. Use the “Blank Site” site template.
2. In Visual Studio 2010 we create a Empty SharePoint Project. I named it “ik.SharePoint2010.Workflow”
We create it with “Deploy as farm solution” and specify the location “http://sharepoint.local/sites/workflow/”.
3. This is the project structure at start:
4. First we need to create an instance of the Workflow History list that is needed for the workflow.
Create a new “Empty Element” project item named “Workflow History”.
Open the created “element.xml” file and modify it as shown below.
The “TemplateType” attribute represents the "Workflow History” list template. It resists on a SharePoint feature with ID “00BFEA71-4EA5-48D4-A4AD-305CF7030140”. It’s a native SharePoint feature.
You can add the attibute “Hidden” to the “ListInstance” tag and set it’s value to “TRUE” to hide the list as it’s done by SharePoint by default for this list. In this case you should also change “OnQuickLaunch” to “FALSE”. For my demo purpose I want to have “Workflow History” visible and on the Quick Launch bar.
5. Now we will create all tools we need for a “Workflow 1”. (May be I’ll create more workflow demos later. So it’s number 1.)
6. We create the SharePoint fields for “Workflow 1”. Therefore we create another “Empty Element” project item named “Workflow 1 Fields”.
I’ll create 3 fields for use in the Task Form we will create: Test1, Test2, Test3. They are all fields of type Text.
7. Now we create a new “Module” project item named “Workflow 1 Forms”. In this module we will store the Task Form.
Remove the “Sample.txt” file from the created module.
Create a new “Application Page” project item named “Task1Form.aspx”.
This project item will stored in the folder “Layouts\ik.SharePoint2010.Workflow”.
Move the project item “Task1Form.aspx” using Drag & Drop into the module “Workflow 1 Forms”.
Remove the “Layouts” folder from the project. It should be empty.
8. We open “Task1Form.aspx”
First we need to edit the “Page” tag of the ASPX site.
Now we insert the following code into the “PlaceHolderMain” content placeholder.
Now we add some ASP.NET code into the “PlaceHolderPageTitle” content placeholder.
Furthermore we add this lines of code into the “PlaceHolderPageTitleInTitleArea” content placeholder.
At least we add the following code into the “PlaceHolderAdditionalPageHead” content placeholder.
You can see the input fields for the three fields of Task 1. Furthermore you see three buttons. For them we now create some “code behind”.
We need to modify the “Elements.xml” file of the module named “Workflow 1 Forms”.
This specifies there to store the “Task1Form.aspx” file in the site structure.
9. In the next step we create the task list that will contain our workflow tasks.
First create a “List Definition” project item named “Workflow 1 Tasks”.
Use “Workflow 1 Tasks” as name of the list definition and “Tasks” as base type. Check “Add a list instance…”.
10. Now open “Elements.xml” of the new list definition project item.
We need to change the identifier of the list type we create! – It must be “107”. This is the list template ID for workflow tasks lists in SharePoint. The workflow designer will search for a list with this type inside the site where a new workflow will be created.
Here is the content of “Elements.xml” after our modification:
11. Now we modify the “Elements.xml” file of the list instance that will be created during deployment:
Here we also need to modify the type identifier to “107”. Furthermore we change the list url: “Lists/Workflow1Tasks”.
Here is the complete content of “Elements.xml”:
In a “real world” scenario we would prevent the list from being listed on the Quick Launch bar. So the corresponding parameter must be set to “FALSE”.
12. Now we need to modify the “Schema.xml” file of the list definition.
First we set the list type to “107” and configure some other attributes:
Now remove the the content types defined in the “ContentTypes” tag in the “schema.xml” file.
Insert this content type definition into the “ContentTypes” tag:
You see the “FormUrls” tag? Inside this tag we specify our custom form template we created before.
The new content type is derived from the “Workflow Task” content type 0×010801. – In the “FieldRefs” section we add our fields we need inside the workflow.
13. Now we need to add the field definitions of our custom fields to the “Schema.xml”. Copy them from the “Elements.xml” file of project item “Workflow 1 Fields” into the “Fields” tag of “Schema.xml”:
Furthermore you need to add a field definition for the field “Completed”:
14. Now deploy the project.
While deployment you may get this dialog:
Check “Do not prompt…”. Press “Resolve Automatically” – it’s your only option .
15. Have a look into the site using the browser. – We will test our “Edit” form. Remember that we did not specify special “New” form or “Display” form. This you could do the same way as you created the “Edit” form.
We see our lists in the Quick Launch.
Open the “Workflow 1 Tasks” list. On the Ribbon open the “Items” tab. We see our Content Type in the New Item submenu:
Create an item of this type. You see a standard “New” form and on it you see our three “Test” fields:
Enter some data and press “Save”.
Open the item in “Edit” form. Now you should see our custom list form.
If you click “Save As Draft” your changes will be stored in the task item. If you click “Complete Task” two item fields will be changed in addition to the changes made in the form: It sets “Status” to “Tasks_Completed” and "% Complete" to "100". You can see this in the Code Behind file of the list form.
Test all buttons.
After “Complete Task”:
You see: “% Complete” is set to “100 %”.
So far our projects works as expected.