April 23, 2008

Starting a SharePoint out of the box Approval Workflow Programmatically

This was something I needed to do for the client, where I had to start the out of the box approval workflow to make use of all the facilities provided by doing so (ability to edit task within outlook, ability to reassign tasks and change task ownership).

Prerequisites: you need to associate the default approval workflow with a list, call it "Approval".

Then in your visual studio workflow add a code activity and include the following code snippet:

SPWorkflowAssociationCollection associationCol = WorkflowProperties.List.WorkflowAssociations;
foreach (SPWorkflowAssociation association in associationCol)
{
    if (association.Name.Equals("Approval"))
    {
        StringBuilder allApprovers = new StringBuilder();
        foreach (string approver in Approvers)
        {
            allApprovers.Append("<my:Person>");
            allApprovers.Append(string.Format("<my:DisplayName>{0}</my:DisplayName>", approver));
            allApprovers.Append(string.Format("<my:AccountId>{0}</my:AccountId>", approver));
            allApprovers.Append("<my:AccountType>User</my:AccountType>");
            allApprovers.Append("</my:Person>");
        }
 
        string associationData =
        "<my:myFields xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:my=\"http://schemas.microsoft.com/office/infopath/2003/myXSD\" xml:lang=\"en-us\">" +
        "<my:Reviewers>" +
        allApprovers +
        "</my:Reviewers>" +
        "<my:CC></my:CC>" +
        "<my:DueDate xsi:nil=\"true\"></my:DueDate>" +
        "<my:Description></my:Description>" +
        "<my:Title></my:Title>" +
        "<my:DefaultTaskType>1</my:DefaultTaskType>" +
        "<my:CreateTasksInSerial>true</my:CreateTasksInSerial>" +
        "<my:AllowDelegation>true</my:AllowDelegation>" +
        "<my:AllowChangeRequests>true</my:AllowChangeRequests>" +
        "<my:StopOnAnyReject xsi:nil=\"true\"></my:StopOnAnyReject>" +
        "<my:WantedTasks xsi:nil=\"true\"></my:WantedTasks>" +
        "<my:SetMetadataOnSuccess>false</my:SetMetadataOnSuccess>" +
        "<my:MetadataSuccessField></my:MetadataSuccessField>" +
        "<my:MetadataSuccessValue></my:MetadataSuccessValue>" +
        "<my:ApproveWhenComplete>false</my:ApproveWhenComplete>" +
        "<my:TimePerTaskVal xsi:nil=\"true\"></my:TimePerTaskVal>" +
        "<my:TimePerTaskType xsi:nil=\"true\"></my:TimePerTaskType>" +
        "<my:Voting>false</my:Voting>" +
        "<my:MetadataTriggerField></my:MetadataTriggerField>" +
        "<my:MetadataTriggerValue></my:MetadataTriggerValue>" +
        "<my:InitLock>false</my:InitLock>" +
        "<my:MetadataStop>false</my:MetadataStop>" +
        "<my:ItemChangeStop>false</my:ItemChangeStop>" +
        "<my:GroupTasks>false</my:GroupTasks>" +
        "</my:myFields>";
        association.SetHistoryList(WorkflowProperties.HistoryList);
        association.SetTaskList(WorkflowProperties.TaskList);
        SPWorkflow approvalWorkflow = wfManager.StartWorkflow(WorkflowProperties.Item, association, associationData, true);
        break;
    }
}

No comments:

Post a Comment