Saturday, August 6, 2011

How To Update Workflow Task Status

Sometimes in sharepoint, you might have faced an issue, that workflow being completed, but the task status is still shows "In Progress".

In that scenario, a simple workaround would be updating the task programmatically for that particular item.

Here you go,
Make sure you update the task for the required item. the below sample updates the task for item id=3
If the item has more than one task, make sure you update the task status to the appropriate task.


       string SiteCollectionUrl = "http://SiteURL/";

            using (SPSite site = new SPSite(SiteCollectionUrl))
            {
                using (SPWeb oWeb  = site.OpenWeb())
                {
                    SPList oList = oWeb.Lists["LISTNAME"];
                    SPListItem item = oList.Items.GetItemById(3);
                    SPWorkflowTaskCollection taskCol = item.Tasks;
                    SPWorkflowTask taskedit = item.Tasks[0];

                    if (taskedit == null)
                        return;
                    Hashtable ht = new Hashtable(); 
                    ht["TaskStatus"] = "#";    // To make the task "approved"
                    SPWorkflowTask.AlterTask((taskedit as SPListItem), ht, true);
                   
                }
            }





ht["TaskStatus"] = "#";   // To Approve the the task use "#"
ht["TaskStatus"] = "@";   // To Reject the the task use "@"



Happy Coding :)

No comments:

Post a Comment