Showing posts with label task. Show all posts
Showing posts with label task. Show all posts

Saturday, August 6, 2011

How to unlock the locked tasks in Sharepoint Workflow

Sometimes in Sharepoint Workflow, tasks will get locked and you will not be able to proceed further.

These locks are placed to prevent the task being updated simultaneously. The workflow runtime locks tasks by setting a field (WorkflowVersion)and persisting that to the database.

if workflowversion is 1 then the tasks are "not locked". Any value apart from "1" symbolises that these tasks are "locked".

Ideally to remove the locks, we need to update WorkflowVersion of the task to "1".

Here you go, the sample for it.


public static void UnlockWorkflowTasks(string siteUrl, string webUrl, string listName)
        {
            using (SPSite site = new SPSite(siteUrl))
            {
                using (SPWeb web = site.OpenWeb(webUrl))
                {
                    SPList list = web.Lists[listName];
                    SPListItemCollection items = list.Items;

                    foreach (SPListItem item in items)
                    {
                        SPWorkflowCollection workflows = item.Workflows;
                        foreach (SPWorkflow workflow in workflows)
                        {
                            SPWorkflowTaskCollection tasks = workflow.Tasks;
                            foreach (SPWorkflowTask task in tasks)
                            {
                                if (task[SPBuiltInFieldId.WorkflowVersion].ToString() != "1")
                                {
                                    task[SPBuiltInFieldId.WorkflowVersion] = 1;
                                    task.SystemUpdate();
                                }
                            }
                        }
                    }
                }
            }
        }


Happy Coding :)

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 :)