Monday, September 9, 2013

SharePoint Start Workflow on All Items of a List via PowerShell

To start a List Workflow in SharePoint on All Items of a list is a pain through GUI but it's a piece of cake in PowerShell. Note that the Workflow will not start instantly (as opposed to triggering through the UI) but in maximum 5 minutes because it's started through the SharePoint Timer Service.


#updated on 10/17/2013

# URL of the Site
$web = Get-SPWeb -Identity "https://sharepointsrv/site1"

$manager = $web.Site.WorkFlowManager

# Name of the list
$list = $web.Lists["Shared Documents"]

# Name of the Workflow
$assoc = $list.WorkflowAssociations.GetAssociationByName("On Item Created","en-US")

$data = $assoc.AssociationData
$items = $list.Items
foreach($item in $items)
 {
 $wf = $manager.StartWorkFlow($item,$assoc,$data,$true)
 }
 
$manager.Dispose()
$web.Dispose()
#

References

8 comments:

  1. I'm trying to use this script with SharePoint 2013, but am running into an error on every item in the list.

    Exception calling "StartWorkflow" with "4" argument(s): "Value cannot be null."

    I believe it has to do with the "$assoc = $list.WorkflowAssociations.GetAssociationByName" coming back empty. I'm doing a write-host on each variable but the $assoc and $data are coming up blank. Any thoughts?

    ReplyDelete
  2. I am getting error as below:


    PS C:\Users\Administrator> & "D:\HoneywellTemp\powershellscript.ps1"
    Cannot index into a null array.
    At D:\HoneywellTemp\powershellscript.ps1:9 char:20
    + $list = $web.Lists[ <<<< "LOB wise Count"]
    + CategoryInfo : InvalidOperation: (LOB wise Count:String) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

    You cannot call a method on a null-valued expression.
    At D:\HoneywellTemp\powershellscript.ps1:12 char:57
    + $assoc = $list.WorkflowAssociations.GetAssociationByName <<<< ("email","en-US")
    + CategoryInfo : InvalidOperation: (GetAssociationByName:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    You cannot call a method on a null-valued expression.
    At D:\HoneywellTemp\powershellscript.ps1:18 char:30
    + $wf = $manager.StartWorkFlow <<<< ($item,$assoc,$data,$true)
    + CategoryInfo : InvalidOperation: (StartWorkFlow:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    You cannot call a method on a null-valued expression.
    At D:\HoneywellTemp\powershellscript.ps1:21 char:17
    + $manager.Dispose <<<< ()
    + CategoryInfo : InvalidOperation: (Dispose:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    Method invocation failed because [System.String] doesn't contain a method named 'Dispose'.
    At D:\HoneywellTemp\powershellscript.ps1:22 char:13
    + $web.Dispose <<<< ()
    + CategoryInfo : InvalidOperation: (Dispose:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

    ReplyDelete
  3. Absolute life saver. thank you very much!

    ReplyDelete
  4. Awesome!
    This worked perfectly for me tosend out an Oncall Rota email for each person in Sharepoint 2013 Foundation.

    So that I only ran the workflow on the 1st and current week, I addded the following to the $items Variable.

    $items = $list.Items | Select -First 1

    This meant I didnt spam everyone that was in the Rota List!

    Many thanks.

    ReplyDelete
  5. How to scheduled the siteworkflow daily any option available in sharepoint2013 without using external app.

    ReplyDelete
  6. How to scheduled the siteworkflow daily any option available in sharepoint2013 without using external app.

    ReplyDelete
  7. Sharepoint has been a useful tool for us because we get to share one file in one location. Especially when we need a project to be completed, we collaborate our ideas and input it on one file. Unlike the other tools via Edusson rating which are not useful at all.

    ReplyDelete