AnsweredAssumed Answered

Cancelling Site Workflows works with PowerShell but Not C#

Question asked by sonisick on May 25, 2016

For reasons due to a non typical Frontend Servers Utilizing Secure Linux Servers to our SharePoint System normal the Nintex web services won't work in our Production Environment thus I'm having to reinvent the wheel through .Net Code.  If this is not in your mainstream of Nintex programming, the following will expose a few other ways of skinning the cat.

 

I have been able to kill Nintex Workflow Processes through PowerShell but not with a similarly structured C# Console App--which is the way I ultimately need to configure my interface.

 

The following is the PowerShell Script which works for a workflow instance which I captured from Nintex (This variable has to be changed for each one I want to kill--this has not been optimized for a input parameter.)

 

#CancelSiteWorkflow

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

cls

$siteurl = "<my site Url>"

$TestInstanceID = [GUID]("30637c86-2762-44c5-8e63-cb6fcb6eaa9b") #guid from a Review

$web = Get-SPWeb $siteurl

$web.AllowUnsafeUpdates = $true

foreach ($workflow in  $web.Workflows)

{

        if ($workflow.InstanceId -ne $TestInstanceID)

        {

       }

        else

        {

           $workflow.ParentAssociation.Name

            foreach ($Task in $workflow.Tasks)

            {

                $Task["Status"] = "Canceled"

                $Task.Update();

             }

           [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($workflow);

            write-host -f green  "--Workflow Terminated"

    }

}

 

 

$web.Dispose()

 

This works pretty as a picture. I used it to kill a Flexitask that emailed. I get a nice message saying my email response is no longer required and I see the task in the Cancelled List in Workflow History.

 

C# This doesn't work

 

        static void Main(string[] args)

        {

            var TestInstanceID = new Guid("30637c86-2762-44c5-8e63-cb6fcb6eaa9b");

            try

            {

                SPSecurity.RunWithElevatedPrivileges(delegate ()

                {

                    using (SPSite site = new SPSite("http://hcmsspd08/sites/ocrs"))

                    {

                        using (SPWeb web = site.OpenWeb())

                        {

                            web.AllowUnsafeUpdates=true;

 

                            // stop site workflows

                            foreach (SPWorkflow workflow in web.Workflows)

                            {

                                if (workflow.InstanceId != TestInstanceID)

                                {

                                    continue;

                                }

                                else

                                {

                                    foreach (SPWorkflowTask t in workflow.Tasks)

                                    {

                                        t["Status"] = "Canceled";

                                        t.Update();

                                    }

                                    SPWorkflowManager.CancelWorkflow(workflow);

                                }

                            }

                        }

                    }

                });

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.InnerException);

            }

        }

 

No Error Messages. Seems to execute OK. I know the Task disappears from my Workflow Tasks.

Any guru's see my problem.

Outcomes