Skip to main content

Cancel the Workflow Through Object Model



 SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite osite = new SPSite(SPContext.Current.Site.ID))
                    {
                        using (SPWeb oweb = osite.OpenWeb(SPContext.Current.Web.ID))
                        {

                           
                            SPList oList= oweb.Lists["List Name"];
           SPListItem olstitem = oTravelRequestList.GetItemById(133);
                            SPWorkflowManager manager = osite.WorkflowManager;

                            foreach (SPWorkflow itemWorkflow in manager.GetItemActiveWorkflows(olstitem))
                            {
                                MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:txtNotes", NamespaceManager).SetValue("Title is" + manager.GetItemActiveWorkflows(olstitem).Count);
                               
                                oweb.AllowUnsafeUpdates = true;
                                itemWorkflow.CreateHistoryEvent((int)SPWorkflowHistoryEventType.WorkflowCancelled, 0, oweb.CurrentUser , "Cancelled by the User", "Cancelled By Siva", null);
                                oweb.AllowUnsafeUpdates = false;

                                if (!itemWorkflow.IsLocked)
                                {
                                    foreach (SPWorkflowTask taskWorkflow in itemWorkflow.Tasks)
                                    {

                                        taskWorkflow["Status"] = "Canceled";
                                        oweb.AllowUnsafeUpdates = true;
                                        taskWorkflow.SystemUpdate();
                                        oweb.AllowUnsafeUpdates = false;
                                    }
                                   
                                    oweb.AllowUnsafeUpdates = true;
                                    SPWorkflowManager.CancelWorkflow(itemWorkflow);
                                    oweb.AllowUnsafeUpdates = false;

                                               
                                }
                            }
                        }
                    }
                });

Comments

Popular posts from this blog

SharePoint Farm Installation

Windows Cluster : http://technet.microsoft.com/en-us/library/bb727114.aspx http://blogs.msdn.com/b/clustering/archive/2008/01/18/7151154.aspx SQL Cluster : http://clusteringformeremortals.com/2009/10/07/step-by-step-configuring-a-2-node-multi-site-cluster-on-windows-server-2008-r2-%E2%80%93-part-3/ Network Load Balancing http://www.jppinto.com/2009/05/install-and-configure-wlbs-nlb-on-windows-server-2008/ SharePoint 2010 Farm Installation http://www.khamis.net/blog/Lists/Posts/Post.aspx?ID=48