Wednesday, October 21, 2015

Kickoff SharePoint 2013 Worklfows using PowerShell

Here is the script I created and used for triggering SP 2013 Worklfows. This does not work for 2010 workflows. You can schedule this script on Task Scheduler on any Machine installed with SharePoint bits

cls
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

$sourceWebURL = 'http://xxx.com/sites/xxx/subsite'
$sourceListName = 'TaskNotifications'
$TargetWorkflow = 'NotifyPendingTasks'
$spSourceWeb = Get-SPWeb $sourceWebURL
$spSourceList = $spSourceWeb.Lists[$sourceListName]
$items = $spSourceList.getItems()

#-- Getting a Workflow manager object to work with.
$wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($spSourceweb)
#-- Getting the subscriptions
$sub = $wfm.GetWorkflowSubscriptionService()
#-- Getting the specific workflow within the list of subscriptions on the specific list. (SP2010 associated workflows basically)
$WF = $sub.EnumerateSubscriptionsByList($spSourcelist.ID) | Where-Object {$_.Name -eq "$TargetWorkflow"}
#-- Getting a Workflow instance in order to perform my commands.
$wfis=$wfm.GetWorkflowInstanceService()


Foreach($item in $items){
 
    $object = New-Object 'system.collections.generic.dictionary[string,object]'
    $object.Add("WorkflowStart", "StartWorkflow");
   $wfis.StartWorkflowOnListItem($WF, $item.ID, $object)
}

1 comment: