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

Mention a Channel or Team – Power Automate

Mention a Channel or Team – Power Automate graph.microsoft.com/v1.0/teams/{teamId}/channels/{channelId}/messages Channel: ---------- {   &qu...

Popular posts