Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

hope this helps:

$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).

share|improve this answer
    
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 at 17:02
    
I got it working with 2010 workflows, just not 2013 workflows –  zingwing Aug 11 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 at 8:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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