We know that SharePoint workflows are driven by tasks, and in real-world scenarios, we might want to have more flexibility in the way the workflow tasks are handled, such as batch processing, IM software processing tasks such as Office Communicator, and so on, and we You need to programmatically finish the task externally.
As a normal idea, SharePoint's Task list is also a splist, so we'll try this:
SPSite site = new SPSite("http://windstyle");
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
SPList taskList = web.Lists["任务"];
SPListItem task = taskList.Items[0];
task["状态"] = "已完成";
task["Completed"] = true;
task.Update();
web.AllowUnsafeUpdates = false;
After execution, the discovery task has indeed been set to the completed state, but the workflow does not continue, that is, the workflow's ontaskchanged activity does not capture the event that the task was changed.
In fact, the SharePoint object model provides a way to change the properties of a workflow task, Spworkflowtask.altertask, not only to change the properties of a workflow task, but also to notify the corresponding workflow so that the corresponding workflow can continue to execute.
The use of this method is also simple:
SPSite site = new SPSite ("Http://windstyle");
SPWeb Web = site. OpenWeb ();
SPList doclib = web. lists["Shared Documents"];
SPListItem doc = doclib. Items[0];
Spworkflowtaskcollection tasks = doc. Workflows[doc. WORKFLOWS.COUNT-1]. Tasks; Find the specific spworkflowtask
Hashtable ht = new Hashtable ();
Ht. ADD (spbuiltinfieldid.completed, true); Set the task to complete
Ht. Add (spbuiltinfieldid.taskstatus, completed);//Set the display status of the task in the task list to completed
Ht. ADD (Spbuiltinfieldid.percentcomplete, 1); Set the percent complete of the task in the task list to "100%"
Ht. ADD (spbuiltinfieldid.workflowoutcome, "Some output infomation"); Sets the output message for a task, equivalent to the Completeworkflow active Taskoutput Property
Spworkflowtask.altertask (Tasks[0], HT, when the workflow is designed with Visual Studio. true);
As you can see from the previous code, it's not difficult to get the task done programmatically, which is the tricky part of finding the right workflow task. Because splistitem.workflows may contain more than one spworkflow, these spworkflow may originate from the same spworkflowtemplate and may originate from different spworkflowtemplate. It may also contain multiple spworkflow from the same spworkflowtemplate, and of course we know that the same workflow template can only start one workflow instance on the same SPListItem, So only one of these Spworkflow iscompleted property is true.
So when you're writing code to complete a workflow task, be aware of getting the right workflow tasks, and then you can easily complete the task.