Provision SharePoint OOTB Approval Workflow programmatically in Visual Studio

In this article, I would like to show you on how to provision SharePoint out of the box Approval Workflow programmatically in Visual Studio.

In this scenario, I will provision the approval workflow when Feature Activated. So the feature receiver will look like:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
    SPWeb web = properties.Feature.Parent as SPWeb;
    SPList listTemplateManagement = web.Lists["Template Management"];

    // Create Approval Workflow and associate it with the library
    SPWorkflowTemplate baseTemplate = web.WorkflowTemplates.GetTemplateByName("Approval - SharePoint 2010", CultureInfo.CurrentCulture);
    SPWorkflowAssociation assoc = SPWorkflowAssociation.CreateListAssociation(baseTemplate, "Template Management Approval Workflow", web.Lists["Workflow Tasks"], web.Lists["Workflow History"]);

    // Checked: Allow this workflow to be manually started by an authenticated user with Edit Item permissions.
    assoc.AllowManual = true;

    // Checked: Require Manage Lists Permissions to start the workflow.
    if (assoc.AllowManual)
        SPBasePermissions emptyMask = SPBasePermissions.EmptyMask;
        emptyMask |= SPBasePermissions.ManageLists;
        assoc.PermissionsManual = emptyMask;

    assoc.AllowAsyncManualStart = false;
    assoc.AutoStartCreate = false;
    assoc.AutoStartChange = false;
    string data = GetApprovalWorkFlowData();
    assoc.AssociationData = data;

    // Checked: Start this workflow to approve publishing a major version of an item.
    listTemplateManagement.DefaultContentApprovalWorkflowId = assoc.Id;

private string GetApprovalWorkFlowData()
    string result = string.Empty;
    result = ""
        + " <dfs:myFields xmlns:xsd=\"\" xmlns:dms=\"\" xmlns:dfs=\"\" xmlns:q=\"\" xmlns:d=\"\" xmlns:ma=\"\" xmlns:pc=\"\" xmlns:xsi=\"\">"
        + " <dfs:queryFields></dfs:queryFields>"
        + " <dfs:dataFields>"
        + " <d:SharePointListItem_RW>"
        + " <d:Approvers>"
        + " <d:Assignment>"
        + " <d:Assignee>"
        + " <pc:Person>"
        + " <pc:DisplayName>DMS Template Owners</pc:DisplayName>"
        + " <pc:AccountId>DMS Template Owners</pc:AccountId>"
        + " <pc:AccountType>SharePointGroup</pc:AccountType>"
        + " </pc:Person>"
        + " </d:Assignee>"
        + " <d:Stage xsi:nil=\"true\" />"
        + " <d:AssignmentType>Parallel</d:AssignmentType>"
        + " </d:Assignment>"
        + " </d:Approvers>"
        + " <d:ExpandGroups>false</d:ExpandGroups>"
        + " <d:NotificationMessage>Please review the updated template.</d:NotificationMessage>"
        + " <d:DueDateforAllTasks xsi:nil=\"true\" />"
        + " <d:DurationforSerialTasks>3</d:DurationforSerialTasks>"
        + " <d:DurationUnits>Day</d:DurationUnits>"
        + " <d:CC />"
        + " <d:CancelonRejection>true</d:CancelonRejection>"
        + " <d:CancelonChange>true</d:CancelonChange>"
        + " <d:EnableContentApproval>true</d:EnableContentApproval>"
        + " </d:SharePointListItem_RW>"
        + " </dfs:dataFields>"
        + " </dfs:myFields>";
    return result;

Next question is … How do you figure out the AssociationData xml? I create the OOTB Approval Workflow manually and Create simple Console Application to retrieve the AssociationData xml.

static void Main(string[] args)
        using (SPSite site = new SPSite("http://yoursite"))
            using (SPWeb web = site.OpenWeb("yourweb"))
                SPList listTM = web.Lists["Template Management"];
                SPWorkflowAssociation assoc = listTM.WorkflowAssociations[0];
                string xmlResult = assoc.AssociationData;
    catch (Exception ex)
        Console.WriteLine("ERROR: " + ex.Message);

Tagged: , , , , , , , ,

11 thoughts on “Provision SharePoint OOTB Approval Workflow programmatically in Visual Studio

  1. Akshit mathur 10/15/2013 at 6:30 AM Reply

    Nice Article…Can you please tell me of I need to change the Email Body of the Workflow what should I do.I have two stage approval process,and I need to change the Email body when First approver Approves.

  2. Sanjay Bhagia (@bhagiasanjay) 10/17/2013 at 9:06 AM Reply

    How can I fetch workflowtemplate for language other than english? If I have my web created on any other language than how to look for the template? BaseId also varies from language to language.


    • tjenho 10/17/2013 at 10:10 AM Reply

      Have you tried to loop through web.WorkflowTemplates in console app and display the id and name?

  3. Sanjay Bhagia (@bhagiasanjay) 10/17/2013 at 10:30 AM Reply

    yes, i have tried. BaseId is not the same

  4. tjenho 10/17/2013 at 10:43 AM Reply

    So, you might want to capture all varieties of BaseId depends on your selected culture info.
    switch(cultureinfo.currentculture) { case : … default : … }

    Sorry if I don’t understand your question correctly?

  5. Sanjay Bhagia (@bhagiasanjay) 10/17/2013 at 11:27 AM Reply

    No, actually in my case, I have sites created on different languages and i have my custom list to which i would like to associate OOB approval workflow.

    I need to fetch the approval workflow template based on either id or name. Id/BaseIds are not the same and i’m not able to find out the token/resource to fetch the name either.


  6. tjenho 10/17/2013 at 11:51 AM Reply

    Like you said, approval workflow base id is different from one language to another. I don’t know if there is an OOB method exists to retrieve respective approval workflow base id based on current culture info.

    If not, unfortunately you have to do like I mentioned before using switch case. I can see you’ve already asked the question in other forum, so hopefully someone will response it.

  7. Sanjay Bhagia (@bhagiasanjay) 10/17/2013 at 6:23 PM Reply

    Thanks tjenho. I have found the solution to this problem. I’ll write the blog post on this, will post it tomorrow here as well 🙂

  8. Sanjay Bhagia (@bhagiasanjay) 10/20/2013 at 11:11 AM Reply

    hi tjenho,
    I just wrote the blog post on this issue:

    • tjenho 10/21/2013 at 7:09 AM Reply

      Great … nice to know

Leave a Reply

Please log in using one of these methods to post your comment: 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

%d bloggers like this: