Wednesday, 12 October 2016

Powershell: starting a workflow on items in list within a view

I had so much trouble getting this to work I thought I would share this.

Limitation of this you can only process the items within the page of the view so you need to change your max page number to 1000.


#web site url
$siteurl="https://portal.peelschools.org/MLP"
#list name
$ListName="AODAACCE"
#workflow Name
$workflowName="DBUpdateReusable2010WF"
#view name Note: Change the view limit to 1000. This script is limited the page in a view.
$viewName="wf"

#content type name
$ctname="MLPRequiredAction"
$cTypes = $list.ContentTypes
$cType = $cTypes[$ctname]

$web = Get-SPWeb -Identity $siteurl
$manager = $web.Site.WorkFlowManager

$List = $Web.lists[$ListName]
$view = $list.Views[$viewName]

# Name of the Workflow
$assoc = $cType.WorkflowAssociations.GetAssociationByName($workflowName,"en-US")

$web.AllowUnsafeUpdates = $true
$wf = $cType.WorkflowAssociations
$data = $wf.AssociationData


write-host $view.Url
foreach($item in $list.GetItems($view))
{

 $mywf = $manager.StartWorkFlow($item,$assoc,$data,$true)

}
$manager.Dispose()
$Web.Dispose()


Error message 
Exception calling "StartWorkflow" with "4" argument(s): "This workflow association is not valid for use with this item."
At line:33 char:2
+  $mywf = $manager.StartWorkFlow($item,$assoc,$data,$true)
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

I had to add  the following line to resolve the error.
$web.AllowUnsafeUpdates = $true

No comments:

Post a Comment