Tuesday, November 27, 2012

How to cancel running workflows in a sharepoint list using server code.

SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    web = site.OpenWeb();
                    SPWorkflowManager manager = site.WorkflowManager;
                    SPList list = web.Lists["mySPList"];
                    SPQuery q = new SPQuery();
                    q.Query = "<Where><Eq><FieldRef Name='EmpId'/><Value Type='Number'>" +Convert.ToInt32(lblEmpId.Text) + "</Value></Eq></Where>";
                   SPListItemCollection collection = list.GetItems(q);
                    foreach (SPListItem item in collection)
                    {
      foreach (SPWorkflow workflow in manager.GetItemActiveWorkflows(item))
                        {
                            foreach (SPWorkflowTask t in workflow.Tasks)
                            {
                                t["Status"] = "Canceled";
                                t.Update();
                            }
                            SPWorkflowManager.CancelWorkflow(workflow);
                        }
                        break;
                    }
                });

No comments:

Post a Comment

Featured Post

Create SharePoint Folder Structure in Destination (Only If Not Exists)

Why This Script Is Safe You can run it multiple times It will not create duplicate folders It will only create missing folders S...

Popular posts