I have a sequential workflow which creates a task that contains the custom url to a custom webpart.

The web part provides the option to select different statuses. When the user selects 'Approved' I want to mark the respective task as completed.

How can I get this done in c#?

1 Answer 1


you can get the idea from the code below

SPSite site = new SPSite("http://servername/");
using (SPWeb web = site.OpenWeb())
  SPList list = web.Lists["TestList"];
  SPListItem item = list.GetItemById(22);
  SPWorkflow workflow = item.Workflows[0];
  SPWorkflowTask task = workflow.Tasks[0];

  Hashtable ht = new Hashtable();       
  ht[SPBuiltInFieldId.Completed] = "TRUE";
  ht["Completed"] = "TRUE";
  ht[SPBuiltInFieldId.PercentComplete] = 1.0f;
  ht["PercentComplete"] = 1.0f;
  ht["Status"] = "Completed";
  ht[SPBuiltInFieldId.TaskStatus] = SPResource.GetString(new CultureInfo((int)task.Web.Language, false), Strings.WorkflowStatusCompleted, new object[0]);
  ht[SPBuiltInFieldId.WorkflowOutcome] = "Approved";
  ht["TaskStatus"] = "Approved";
  ht["FormData"] = SPWorkflowStatus.Completed;

  web.AllowUnsafeUpdates = true;
  SPWorkflowTask.AlterTask((task as SPListItem), ht, true);

Source here

  • @Rami-shareef I tried the same with a SharePoint 2013 workflow, It dint work. I am gettin an argument out of range exception in item.Workflows[0]. Do you have any ideas on that?
    – Muhammedh
    Commented Oct 1, 2014 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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