none
Powershell script to find Sharepoint 2013 Workflows shows Empty\Null

    Question

  • I'm trying to write a script to restart some workflows. I don't seem to be able to get the workflows associated with the item despite the fact they are indeed running. The problem is that the Workflows property always shows empty.

    I've used many examples from the net with no luck.

    #---------------------
    $web = Get-SPWeb "http://sharepoint"
    
    $wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($web)
    $sub = $wfm.GetWorkflowSubscriptionService()
    $list = $web.Lists["Documents"]
    $WF = $sub.EnumerateSubscriptionsByList($list.ID) | Where-Object {$_.Name -eq "Document Review"}
    $wfis=$wfm.GetWorkflowInstanceService()
    $items = $list.Items
    
    
    foreach($item in $items){
    
     $item.Title
     $item.Url
     $item.DisplayName
     $item.Workflows
    
    }


    David Jenkins

    Thursday, November 19, 2015 9:01 PM

All replies

  • Hi David L,

    From your description, my understanding is that PowerShell script to find SharePoint 2013 Workflows shows Empty.

    $WF = $sub.EnumerateSubscriptionsByList($list.ID) | Where-Object {$_.Name -eq "Document Review}Which means get all SharePoint 2013 platform Workflows associated with a list.

    $item.Workflows

    Which means Get SharePoint 2010 platform Workflow instance.

    To get SharePoint 2013 platform workflow instance, you can use C#, refer to the following article :

    http://ranaictiu-technicalblog.blogspot.jp/2013/05/sharepoint-2013-workflow.html

    Best Regards,

    Lisa Chen


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Friday, November 20, 2015 9:58 AM
    Moderator
  • Interesting read, but doesn't seem to help me.

    David Jenkins

    Tuesday, December 1, 2015 2:14 PM
  • I'm really struggling here.  It doesn't seem like I'm getting any useful return back from any of the examples out there.  I'd say that most of the examples do not work or are designed for 2010.

    Does anyone have a working example of powershell script that can get an attached Reusable workflow?


    David Jenkins

    Tuesday, December 1, 2015 3:48 PM
  • I figured this out, but have found nothing online, so wanted to add to this thread.

    bottom line: SharePoint 2013 workflows are very different than 2010 workflows.

    the $item.Workflows collection seems to only be associated with SP2010 workflows, for SP2013 workflows you must use WorkflowServicesManager and WorkflowInstanceService.

    Here is PowerShell to loop through all items, look for suspended SP2013 workflows and terminate them. There are already examples out there for starting a SP2013 WF, so "restarting" is just a matter of calling that method after you terminate.

    # Workflow Services Manager
    $wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($web)

    # Workflow Instance Service
    $wfis=$wfm.GetWorkflowInstanceService()

    # loop through all list items
    foreach($item in $list.items)
    {
        # loop through all workflow instances
        foreach($wfinstance in $wfis.EnumerateInstancesForListItem($list.id, $item.id))
        {
            # is the instance suspended?
            if ($wfinstance.status -eq "Suspended")
            {
                # terminate!
                $wfis.TerminateWorkflow( $wfinstance )
            }
        }
    }


    Thursday, May 19, 2016 7:39 PM