Share via


How to set the Outcome/Workflow Status column to Approved or Rejected while using the SpWorkflowTask.AlterTask API to alter the tasks programmatically?

 

Once I had to work on requirement where I was using SharePoint’s OOB Approval workflow but I had to programmatically alter the workflow task using SPWorkflowTask.AlterTask API. Below is the code snippet that I used. Though I was able to set the percent complete of the task as “completed” but still wasn’t able to set the workflow outcome.

           SPSite mysitecoll = new SPSite("…..");

           SPWeb web = mysitecoll.OpenWeb();

            try

            {

                SPList timesheets = web.Lists["CustomList"];

                string listID = "";

                SPListItem item = timesheets.Items[0];

                SPWorkflowTask taskedit = null;

                SPWorkflowTask task = item.Tasks[0];

                taskedit = task;

                      

                // alter the task

                Hashtable ht = new Hashtable();

                ht["Status"] = "Complete";

                ht["PercentComplete"] = 1.0f;

               

                SPWorkflowTask.AlterTask((taskedit as SPListItem), ht, true);

               

            }

            catch(Exception ex)

            {

                MessageBox.Show(ex.InnerException.ToString());

           

            }

Though it sound simple but that wasn’t the case. I tried with all options (ht["Completed"] = "TRUE"; ht["TaskOutcome"] = "Approved by …"; etc) but no luckL . And finally had my Tech Lead who helped me out with it. Approval workflow requires a property “TaskStatus” to be set in order to make it as Approved or Rejected.

 

OOB Approval workflow uses the TaskStatus values as :-

1) "@" to reject the task
2) "#" to complete the task

Set ht["TaskStatus"] = "#" to set task outcome as “Approved”

Set ht["TaskStatus"] = "@" to set task outcome as “Rejected”

           SPSite mysitecoll = new SPSite("…..");

           SPWeb web = mysitecoll.OpenWeb();

            try

            {

                SPList timesheets = web.Lists["CustomList"];

                string listID = "";

                SPListItem item = timesheets.Items[0];

                SPWorkflowTask taskedit = null;

                SPWorkflowTask task = item.Tasks[0];

                taskedit = task;

                      

                // alter the task

                Hashtable ht = new Hashtable();

                ht["Status"] = "Complete";

                ht["PercentComplete"] = 1.0f;

    ht["TaskStatus"] = "#";

               

                SPWorkflowTask.AlterTask((taskedit as SPListItem), ht, true);

               

            }

            catch(Exception ex)

            {

                MessageBox.Show(ex.InnerException.ToString());

           

            }

The above code marks the "Status" column of the task as 'Complete' and the "Outcome" or the workflow status as "Approved/Rejected"