0

I'm trying to figure out how to use powershell to start a list workflow. I found a few sites that give examples for sharepoint 2010 workflows, but only one for sharepoint 2013 workflows here.

Add-PSSnapin microsoft.sharepoint.powershell
$sourceWebURL = '<URL>'
$sourceListName = '<List Name>'
$TargetWorkflow = '<Workflow Name>'
$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 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 $spSourceList){
    #Creating the dictonary object I need to parse into StartWorkflow. This could be most other workflow commands.
    $object = New-Object 'system.collections.generic.dictionary[string,object]'
    $object.Add("WorkflowStart", "StartWorkflow");
    $wfis.StartWorkflowOnListItem($WF, $item.ID, $object)

When I enter in the correct values the follow error get's thrown.

Cannot convert argument "itemId", with value: "GUID", for "StartWorkflowOnListItem" to type "System.Int32": "Cannot convert the "GUID" value of type "System.Guid" to type 
"System.Int32"."
At line:23 char:5
+     $wfis.StartWorkflowOnListItem($WF, $item.ID, $object)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

Does anyone know what I need to do to fix this error?

2
$web = Get-SPWeb -Identity "http://YourParameter.xx/sites/publishing/en"

$workFlowManager = $web.Site.WorkFlowManager

$list = $web.Lists["YourParameter"]

$workflowAssociations =
$list.WorkflowAssociations.GetAssociationByName("YourParameter","en-US")

$associationData = $workflowAssociations.AssociationData

$items = $list.Items

foreach($item in $items) { $workflow =
$workFlowManager.StartWorkFlow($item,$workflowAssociations,$associationData,$true)
}

$workFlowManager.Dispose()

$web.Dispose()

It works in my SharePoint 2013 farm (starts workflow for all items).

  • When I run that code I get the following error Exception calling "StartWorkflow" with "4" argument(s): "Value cannot be null." At line:15 char:28 + foreach($item in $items) { $workflow = $workFlowManager.StartWorkFlow($item,$wor ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ArgumentNullException Do you know how I can resolve the error? – zingwing Aug 11 '14 at 17:02
  • I got it working with 2010 workflows, just not 2013 workflows – zingwing Aug 11 '14 at 19:59
  • Could post the your function please? It seems you are missing one parameter for the method StartWorkflow(...). Could you double check if the parameters $item, $workflowAssociations and $associationData are not null? – Laslo Birnstil Aug 12 '14 at 8:55
0

Easy!

Foreach($item in $spSourceList)

That's all because of in an $item you got list but not an item :)

try

foreach ($item in $sourcelist.Items)

The method in second post wont work with 2013 workflows only 2010!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.