1

Here is my code to programmatically alter the task at hand, the only problem is that the code doesn't alter the "Outcome" of the approval task. The Outcome field is blank. "Task Status" is Complete, but the Outcome is empty for that task under the workflow status.

Also, under the Task list, the task at hand is 100% complete and set to Completed

Hashtable data = new Hashtable();
            data["FormData"] = SPWorkflowStatus.Completed;
            data[SPBuiltInFieldId.Completed] = "TRUE";
            data["Completed"] = "TRUE";
            data[SPBuiltInFieldId.PercentComplete] = 1.0f;
            data["PercentComplete"] = 1.0f;
            data["Status"] = "Completed";
            data[SPBuiltInFieldId.TaskStatus] = SPResource.GetString(new CultureInfo((int)task.Web.Language, false),
                                                                    Strings.WorkflowStatusCompleted, new object[0]);
            data[SPBuiltInFieldId.WorkflowOutcome] = "Approved";
            data["TaskStatus"] = "Approved"; 
            data["ows_FieldName_Comments"] = "Approved by " + user.Name + "\n";
            web.AllowUnsafeUpdates = true;
            SPWorkflowTask.AlterTask(task, data, true);
1

I was able to get this working using this blog and I think you have the incorrect assignment to data["TaskStatus"].

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

    // alter the task
    Hashtable ht = new Hashtable();
    ht["Status"] = "Complete";
    ht["PercentComplete"] = 1.0f;
    ht["TaskStatus"] = "#";
    SPWorkflowTask.AlterTask((taskedit as SPListItem), ht, true);
  • tried both ways... – Mike Apr 29 '13 at 13:12
0

In SharePoint 2013 there is no need to use alter the task.

Directly access the list Workflow Tasks and update the item. Following code worked for me:

using (SPSite _site = new SPSite(spSiteURL))
                {
                    using (SPWeb _web = _site.OpenWeb())
                    {
                        SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            _site.AllowUnsafeUpdates = true;
                            SPList _taskList = _web.Lists[spTaskListName];
                            SPListItem _taskItem = _taskList.GetItemById(8);
                            if (_taskItem != null)
                            {
                                _web.AllowUnsafeUpdates = true;
                                _taskItem["TaskOutcome"] = "Approved";
                                _taskItem["PercentComplete"] = "1";
                                _taskItem["Status"] = "Completed";
                                _taskItem.Update();
                                _web.AllowUnsafeUpdates = false;

                            }
                        });
                    }
                }
  • Works for me but when a Workflow is awaiting on the task to be completed. We get an error from the Workflow (FormatException) it's trying to convert the String (TaskOutcome) into a Number. – Hybris95 Nov 29 '16 at 13:45
-1

Answering very late. but following link explains how to approve SharePoint workflow 2013 using c# SSOM

https://timeofcode.wordpress.com/2018/10/13/pragmatically-approve-sharepoint-workflow-2013-c/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.