Friday, February 19, 2016

SharePoint 2013 workflow can't terminate using UI: Terminate using powershell

Had a strange situation where the workflow instance will not terminate using the UI.   When I click on the status, it just gave an error so after tracking down what the error was in the uls which didn't give too much information but that something was a null where a value was expected: 


Getting Error Message for Exception System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.ArgumentNullException: Value cannot be null.


at Microsoft.SharePoint.Workflow.SPWorkflow.retrieveSchema(Schema schemaIndex)


at Microsoft.SharePoint.Workflow.SPWorkflow.retrieveSchemaGuid(Schema schemaIndex)


at Microsoft.SharePoint.Workflow.SPWorkflow.get_AssociationId()


at Microsoft.SharePoint.Workflow.SPWorkflow.GetReadOnlyParentAssociation()


at Microsoft.SharePoint.Workflow.SPWorkflow.get_ParentAssociation()
.......




Here is the powershell to terminate the workflow instance that is suspended.  a slightly modified script from http://social.technet.microsoft.com/wiki/contents/articles/23850.sharepoint-2013-workflow-management-starting-a-workflow-using-powershell.aspx






Add-PSSnapin *sharepoint* -ErrorAction SilentlyContinue


$sourceWebURL = '<url to your web>'
$sourceListName = '<list name>'
$TargetWorkflow = '<workflow name>'
$itemId = <Id of the item the workflow you want to stop>


$spSourceWeb = Get-SPWeb $sourceWebURL
$spSourceList = $spSourceWeb.Lists[$sourceListName]

#-- Getting a Workflow manager object to work with.
$wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($spSourceweb)
#-- Getting a Workflow instance in order to perform my commands.
$wfis=$wfm.GetWorkflowInstanceService()


$wfinstances = $wfis.EnumerateInstancesForListItem($spSourceList.ID, $itemId)
foreach($wfi in $wfinstances)
{
    if($wfi.Status -eq "Suspended")
    {
        $wfi
        $wfis.TerminateWorkflow($wfi)
    }
}

No comments:

Post a Comment