Simple Visual Studio Workflow with Initiation Form

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:

Workflow Initiation Form

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.

Workflow History Outcome

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.

    Empty SharePoint Project

  • Add a Sequential Workflow project item. Name it SeqWorkflow.

    Visual Studio Sequential Workflow

  • Right click on the SeqWorkflow project item, choose Add New Item. Choose Workflow Initiation Form, name it WorkflowInit.aspx.

    Add Workflow Initiation Form

  • Inside PlaceHolderMain content area, construct the the form using existing ASP.NET controls on the toolbox. Here is an example:

    <asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
        <div style="margin: 20px">
            Title: <asp:TextBox ID="TitleText" runat="server"></asp:TextBox> <br />
            <br /> 
            <asp:Button ID="StartWorkflow" runat="server" OnClick="StartWorkflow_Click" Text="Start Workflow" />
            <asp:Button ID="Cancel" runat="server" OnClick="Cancel_Click" Text="Cancel" />
  • 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.

    Workflow Designer Area

  • 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.

Source code: Simple visual studio workflow with initiation form source.
Source: Simple Visual Studio Workflow with Initiation Form


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s