Here's the scenario:

I have a SPListItem in hand, which i know has a started workflow associated. The workflow has a task created for approval.

I want to access the SPWorkflow and then the SPWorkflowTask and update the status of the task via code (Implemented within a web part).

Help please!

Update 1: Tried to access the SPWorkflow object the below mentioned code still no luck. I got an ArgumentOutOfRange exception

SPWorkflow changeRequestWorkflow = currentListItem.Workflows[0];

Update 2


this also returned null

2 Answers 2


You can get workflow as:

if (item != null && item.Workflows != null && item.Workflows.Count > 0)
        var workflows = site.WorkflowManager.GetItemActiveWorkflows(item);
        foreach (SPWorkflow workflow in workflows)
            // match on some indentifiable attribute of your custom workflow
            // the history list title is used below as an example

            if (workflow.ParentAssociation.HistoryListTitle.Equals(Constants.WORKFLOW_HISTORY_LIST_TITLE))
                var workflowTasks = workflow.Tasks;
                if (workflowTasks != null && workflowTasks.Count > 0)
                    foreach(SPWorkflowTask task in workflowTasks)
                    //Check if the task is the manager's approval task
                    if (task[task.Fields["Status"].InternalName].ToString() != "Completed")
                        if (task.Title.Contains("manager"))
                            taskedit = task;
                            Hashtable ht = new Hashtable();
                            ht["Completed"] = "true";
                            ht["Status"] = "Completed";
                            ht["PercentComplete"] = 1.0f;
                            ht["TaskStatus"] = "Approved";

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

                            Console.WriteLine("Task updated");
        // handle error



  • Im getting an ArgumentOutOfRange exception at 'SPListItem item = leaveApplicationList.Items[46];' Item number 46 exists!
    – Muhammedh
    Commented Sep 25, 2014 at 12:44
  • it starts with index 0. 46 is your last number?
    – Aanchal
    Commented Sep 25, 2014 at 12:45
  • Got it... It's a count yeah? So I have 3 items in the list. Hence it will be 2. am i correct?
    – Muhammedh
    Commented Sep 25, 2014 at 12:49
  • My taskCollection count is 0. why is that?
    – Muhammedh
    Commented Sep 25, 2014 at 13:47
  • please see my update.
    – Muhammedh
    Commented Sep 25, 2014 at 14:46

Well I achieved what I wanna do through this => Implementing custom aspx Workflow Task. Hope this will be helpful to someone.

Update 1: How I modified the current task

Inside the application page that is linked with the new content type. I did the below mentioned when my 'Completed' button was clicked.

//Get the SPWorkflow Task item    
var item = SPContext.Current.Item;

item["TaskOutcome"] = "Approved"; //Or Rejected
item["PercentComplete"] = "1";
item["Status"] = "Completed";


Try this code and let me know.

  • will you be able to share some code how exactly you got the spworkflow and workflow task objects?, I am also stuck in same kind of scenario, unable to find anything of that sort in the mentioned blog
    – user4205
    Commented Nov 21, 2014 at 8:49
  • @user4205 please see my updated answer. I have added the code to update the task.
    – Muhammedh
    Commented Nov 25, 2014 at 9:22

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.