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 Game Webpart

http://spgameoflifewebpart.codeplex.com/ Goal: The Game of Life web part was created for a talk I presented at SharePoint Saturday New York in 2009. At the time, SharePoint 2010 was still in Beta and most people hadn't see the Enterprise Metadata Management (taxonomy) features. I used the Game of Life web part as an introduction to the new taxonomy API. I offered to provide the code, so here it is. Requirements ----------------------- - SharePoint Server 2010 with Enterprise Features - Visual Studio 2010 (inc. SharePoint tools) Setup ----------------------- 1. Add your account as a Term Store Administrator. This right is independent to Site Collection Administrator or Farm Administrator status. To open the Term Store Management Tool go to Site Actions > Site Settings > Site Administartion > Term Store Management. You can also  open the same interface from Central Administration > Manage Service Applications > Managed Metadata Service. 2. Run the sol...

CAML Query Helper Class

http://sharepointcamlhelper.codeplex.com/ https://camldotnet.svn.codeplex.com/svn/JohnHolliday.Caml.Net/CAML.cs CAMLManager mgr = new CAMLManager(); mgr.QueryGroups.Add(new QueryGroup("Title", Types.FieldTypes.Text, Types.QueryTypes.Eq, "A Title")); mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.Or, "Title", Types.FieldTypes.Text, Types.QueryTypes.Eq, "Another Title")); mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.And, QueryGroup.MergeTypes.Contain, "Total", Types.FieldTypes.Number, Types.QueryTypes.Geq, "2")); string caml = mgr.GetCAML(); Will provide the following: <Where>   <And>     <Or>       <Eq>         <FieldRef Name="Title" /><Value Type="Text">A Title</Value>       </Eq>       <Eq>         <FieldRef Name="Title" /><Value Type="Text">Another Title</Value>     ...