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

Protect an API by using OAuth 2.0 with Azure Active Directory and API Management

Protect an API by using OAuth 2.0 with Azure Active Directory and API Management https://docs.microsoft.com/en-us/azure/api-management/api-...

Popular posts