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

7 comments:

  1. 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
    Replies
    1. I am getting the same error. Were you able to find any solution? Please help.

      Delete
  2. Absolute life saver. thank you very much!

    ReplyDelete
  3. 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
  4. How to scheduled the siteworkflow daily any option available in sharepoint2013 without using external app.

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

    ReplyDelete