Creating Visual Studio Workflow in SharePoint 2010 is easy. So, don’t make the word Visual Studio scares you. It’s a little bit more difficult than SharePoint Designer workflow, but, trust us, this is not a rocket science. At certain point SharePoint Designer can no longer serve you. At that point and beyond, Visual Studio can be your life saver.
So, in this post we would like to show a very simple SharePoint 2010 Visual Studio Sequential workflow with initiation form. Eh, what is initiation form? It’s an ASP.NET form that is shown when we start the workflow manually. Initiation form gives us a chance to input data to workflow before the workflow is started for a particular item.
The Objective (End Result)
We would like to create a very simple Visual Studio 2010 workflow. This workflow will be associated with a Shared Documents library. When the workflow is started, the following initiation form is displayed:
The “workflow” itself is very simple, there is no process in the workflow that we will build. We’ll keep it very simple at this point. The “workflow” only logs anything we input in the initiation form into the Workflow History list. Here it is.
Step by Step
Here is a little details about how-to create a simple Visual Studio workflow with initiation form. We use Visual Studio 2010 and we use SharePoint 2010. Dare to make the same experiment with Visual Studio 2012/2013 and SharePoint 2013? You are welcome to put a comment in this post.
Create a Visual Studio project using Empty SharePoint Project template.
Add a Sequential Workflow project item. Name it SeqWorkflow.
Right click on the SeqWorkflow project item, choose Add New Item. Choose Workflow Initiation Form, name it WorkflowInit.aspx.
Inside PlaceHolderMain content area, construct the the form using existing ASP.NET controls on the toolbox. Here is an example:
&lt;asp:Content ID=&quot;Main&quot; ContentPlaceHolderID=&quot;PlaceHolderMain&quot; runat=&quot;server&quot;&gt; &lt;div style=&quot;margin: 20px&quot;&gt; Title: &lt;asp:TextBox ID=&quot;TitleText&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:Button ID=&quot;StartWorkflow&quot; runat=&quot;server&quot; OnClick=&quot;StartWorkflow_Click&quot; Text=&quot;Start Workflow&quot; /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;asp:Button ID=&quot;Cancel&quot; runat=&quot;server&quot; OnClick=&quot;Cancel_Click&quot; Text=&quot;Cancel&quot; /&gt; &lt;/div&gt; &lt;/asp:Content&gt;
Open WorkflowInit.aspx.cs, change GetInitiationData() method to return the value of Title TextBox in the form:
// This method is called when the user clicks the button to start the workflow. private string GetInitiationData() return this.TitleText.Text;
Now double-click SeqWorkflow.cs to open workflow design window. Drag LogToHistoryListActivity from Toolbox to workflow design area, below onWorkflowActivated1 activity.
Implement the MethodInvoking event of logToHistoryList activity, use the following code:
private void logToHistoryList_MethodInvoking(object sender, EventArgs e) logToHistoryList.HistoryOutcome = this.workflowProperties.InitiationData;
We’re done. Build and deploy the farm solution, then try.