SharePoint Workflow Development Drip (7)

Source: Internet
Author: User
Tags continue object model visual studio

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.