This is Part 2 of 2 that describes the steps 18 to 27 . You will find Part 1 here: https://blog.kenaro.com/2011/04/24/walkthrough-create-sharepoint-2010-workflow-association-form-and-initiation-form-in-visual-studio-2010-by-using-application-pages-part-1-of-2/
The followings steps describe how to create a new sequential workflow in a Visual Studio 2010 project and associate a Initiation and Association Form with the workflow. Furthermore we will test the project. â In Part 1 we have created:
- a Workflow Task list definition: âWorkflow 2 Tasksâ
- a Workflow Host List definition: âWorkflow 2 Host Listâ
- a Data class: âWorkflow2Dataâ
- a base class "Workflow2DataPagesâ for the association and initiation form.
- a Association Form
- a Initiation Form.
Now we will go on.
Letâs startâŚ
18. Deploy the project – This will create the list instances we need to create the workflow and assign them to lists for debug purpose. Of course you do not need to assign lists (such as Workflow Task List, History List, âŚ) to the workflow at design time in Visual Studio. You could do this later, e.g. in a Feature Receiver.
19. Add a âSequential Workflowâ project item named âWorkflow 2â.
Set âWorkflow 2â as display name and âList workflowâ as type.
Set âWorkflow 2 Host Listâ as âlibrary or list to associate withâ, choose âWorkflow Historyâ in the second dropdown and choose âWorkflow 2 Tasksâ as list for âworkflow tasksâ in the third dropdown.
Now choose only âStart manuallyâ in the next dialog.
20. Now the workflow designer opens.
Double click on âonWorkflowActivated1â. A new class member will be created in the workflows âcodebehindâ file.
Now add the following âusingsâ at the beginning of the file:
using System.IO;
using System.Xml;
using System.Xml.Serialization;
Above the previously created class member âonWorkflowActivated1_Invokedâ add the following code. This will create a dependency property that will be persisted in the workflows data.
(You can use Visual Studio IntelliSense: type âpropdpâ and press âtabâ two times. Then the structure of a new dependency property will be created.)
public Workflow2Data WorkflowDataAssociation
{
get
{
return (Workflow2Data)GetValue(WorkflowDataAssociationProperty);
}
set
{
SetValue(WorkflowDataAssociationProperty, value);
}
}
// Using a DependencyProperty as the backing store for WorkflowDataAssociation.
// This enables animation, styling, binding, etc...
public static readonly DependencyProperty WorkflowDataAssociationProperty =
DependencyProperty.Register("WorkflowDataAssociation", typeof(Workflow2Data), typeof(Workflow_2));
Now add another dependency property of the âInitiationâ data:
public Workflow2Data WorkflowDataInitiation
{
get
{
return (Workflow2Data)GetValue(WorkflowDataInitiationProperty);
}
set
{
SetValue(WorkflowDataInitiationProperty, value);
}
}
// Using a DependencyProperty as the backing store for WorkflowDataInitiation. This enables animation, styling, binding, etc...
public static readonly DependencyProperty WorkflowDataInitiationProperty =
DependencyProperty.Register("WorkflowDataInitiation", typeof(Workflow2Data), typeof(Workflow_2));
We need to do this because Association and Initiation data are stored separatly.
In the method âonWorkflowActivated1_Invokedâ enter this code:
using( StringReader stringReader = new StringReader(workflowProperties.AssociationData) )
{
using( XmlReader reader = XmlReader.Create(stringReader) )
{
XmlSerializer serializer = new XmlSerializer(typeof(Workflow2Data));
WorkflowDataAssociation = (Workflow2Data)serializer.Deserialize(reader);
}
}
using( StringReader stringReader = new StringReader(workflowProperties.InitiationData) )
{
using( XmlReader reader = XmlReader.Create(stringReader) )
{
XmlSerializer serializer = new XmlSerializer(typeof(Workflow2Data));
WorkflowDataInitiation = (Workflow2Data)serializer.Deserialize(reader);
}
}
This will deserialize the workflows association data for using them inside the workflows code.
21. Now we need tho modify the âElements.xmlâ file of the âWorkflow 2â project item.
We add the attribute âAssociationUrlâ and set itâs value to âWorkflow2Forms/Workflow2AssociationForm.aspxâ and add the attribute âInstantiationUrlâ with value âWorkflow2Forms/Workflow2InitiationForm.aspxâ.
22. Now we can deploy the project and see the first results of our (hard) work.
23. Open the URL âhttp://sharepoint.local/sites/workflow/Lists/Workflow2HostListâ in the browser.
Then open the âListâ tab in the Ribbon and click âWorkflow Settingsâ.
Now you should see the associated âWorkflow 2â:
Click them.
At the bottom of the next page you will find the âNextâ button that will open the association form. Click this button.
You see your association form page:
Super! â Press âOKâ.
24. Now we add a new list item to the âWorkflow 2 Host Listâ and start the âWorkflow 2â manually:
âOKâ.
Choose âWorkflowsâ from the context menu of the created list item.
Click âWorkflow 2â.
You see your Initiation Form!!
Click âOKâ and the workflow will start â and will be âCompletedâ.
25. Letâs define a Code Activity to use the initiation data.
In the workflow designer drag a âLogToHistoryListActivityâ activity from the Toolbox pane into the Workflow designer view of âWorkflow 2â and drop it behind âonWorkflowActivated1â.
Now select the âlogToHistoryListActivity1â. In the Properties pane select the property âHistoryDescriptionâ and click the button beside the edit box of this property. In the upcoming dialog we will bind property âData1â of the WorkflowDataAssociation object to the activities property:
Click âOKâ.
Now we add a âDelayâ activity behind âlogToHistoryListActivity1â and set the delay âTimeDurationâ to â00:01:00â. This forces the workflow to delay the execution of the next activities for the defined amount of time.
Now we add another âLogToHistoryListActivityâ and bind itâs âHistoryDescriptionâ property to âWorkflowDataInitiation.Data3â as described above.
(The workflow after adding the two activities)
(Properties of the âdelayActivity1â activity.)
(Properties of the âlogToHistoryListActivity2â.)
26. Deploy the project and open the âWorkflow 2 Host Listâ in the browser. Modify the association of the âWorkflow 2â of the list and enter some information on the association form:
Now add an item to the list, start the âWorkflow 2â on this item and enter some data in the initiation form:
After the workflow starts the information stored in âData1â you entered in the association form will be written to the workflows history:
After about 1 minute the workflow will add another workflow history entry with the content of the âData3â property entered in the initiation form:
27. Thatâs it! â Everything works as expected. â Thanks for reading! –
Please post your comments or question!