How (not) to complete workflow tasks using code in WSS3 |
Developing workflows for Windows SharePoint Services 3 can sometimes be somewhat of a black art. Using ASP.NET provides you with the tools to create customized interfaces, but the exact steps required must be found in other sources. One of the issues I find in some of the content I read on the web relates to the way tasks work in SharePoint and how you can complete them correctly.
There are various ways a user receives feedback on the status of a task. You can set a Status field to Complete, or set the Percent Completed to 100%. A task created by a workflow also has a task outcome field to store custom information regarding the task's completion or failure.
Now first of all, both have little to do with the actual completion of a task as far as SharePoint is concerned. It is for instance possible to have a task status set to Completed while the Percent Completed isn't 100%:
Furthermore, a task can also not be set to 'Completed' in a reliable fashion. Users are allowed to change the possible values of the Status column.
The correct way of completing tasks using code is to use the built-in Completed field, which is hidden from the user and hence provides the right way of completing tasks. You can also get the right localized text for the status column, as long as the values haven't been changed by the user. The SPBuiltInFieldId class provides the right values for columns in the task list. You can use the SPResource class to get the localized text representing the 'Complete' text using the WorkflowTaskStatusComplete resource.
Hashtable data = new Hashtable(); data[SPBuiltInFieldId.Completed] = "TRUE"; data[SPBuiltInFieldId.PercentComplete] = 1; data[SPBuiltInFieldId.TaskStatus] = SPResource.GetString( new CultureInfo( (int)taskListItem.Web.Language, false), "WorkflowTaskStatusComplete", new object[0]);
Hope it helps!