简体   繁体   中英

How to Reassign Workflow Tasks in Workflow Sharepoint 2010?

I'm beginner in SharePoint 2010. I have a SharePoint 2010 workflow that creates a task. The task has custom form and custom code will be executed when the form is submitted. But i would like to re-assigned current task to different user if the actual user cant submit their task.

I found the code at http://sharepoint.aspcode.net/view/635399286724222582137494/correct-way-to-reassign-workflow-tasks-in-visual-studio-wf . The code is following :

web.AllowUnsafeUpdates = true;
                        //SPUser Approver = workflowProperties.Web.AllUsers["To"];
                        int tid = onTaskChanged1_AfterProperties1.TaskItemId;
                        SPListItem currentTaskItem = workflowProperties.TaskList.GetItemById(tid);
                        string TaskApproval = (currentTaskItem["Approval"].ToString());
                        if (TaskApproval.Length > 0)
                        {
                            //workflowProperties.LogComment(TaskApproval, "The item was " + TaskApproval + " by " + Approver.Name);
                            if (TaskApproval == "Reassigned")
                            {
                                Hashtable ht = new System.Collections.Hashtable();
                                ht[SPBuiltInFieldId.WorkflowVersion] = 1;
                                ht[SPBuiltInFieldId.AssignedTo] = currentTaskItem["ReassignedTo"];
                                ht["ReassignedTo"] = null;
                                ht["Approval"] = string.Empty;
                                //currentTaskItem.SystemUpdate();
                                SPWorkflowTask.AlterTask(currentTaskItem, ht, false);
                            }
                            else
                            {
                                //perform actions if item is not reassigned
                            }
                        }

I created two fields which is "Approval" and "ReassignedTo" in Task list. In the Approval Field I placed the value "Reassigned" and in the ReassignedTo field is the value of the user the task is being reassigned to.

but I got this Error :

This task is currently locked by a running workflow and cannot be edited

This is the error that i found :

http://i.imgur.com/4arY4By.jpg

How to rectify this problem? Is it necessary to pause the workflow first before executed the code?

Terminate the workflow (because it really hangs and there's not much you can do with it) and kick off a new one.

Now open the SharePoint 2010 Management Shell (PowerShell) and run the following (or similar code):

$s = Get-SPSite("http://intranet")
$w = $s.RootWeb
$i = $w.GetFile("http://intranet/Shared Documents/Instructions.txt").Item
$t = $i.Tasks[0]
$ht = new-object Hashtable
$ht["TaskStatus"] = "Approved"
[Microsoft.SharePoint.Workflow.SPWorkflowTask]::AlterTask($t, $ht, $false)

This WILL work and correctly finish the approval cycle in any of my tested environments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM