How to start SharePoint list workflow using PowerShell

Crosspost from https://global-sharepoint.com/powershell/how-to-start-sharepoint-list-workflow-using-powershell/

I've used this with great success on SharePoint 2016. I'm cross-posting this here so that I do not lose this info.


How to start SharePoint list workflow using PowerShell automatically?

How to start SharePoint list workflow using PowerShell

 
No comments

In this blog, we will learn how to start the SharePoint list workflow using PowerShell script. Many times, due to the business reasons or technical problems we might need to start the SharePoint list workflow programmatically for all the list items.

Key Highlights

  • How to start the SharePoint list workflow using PowerShell.

PowerShell script to start the SharePoint list workflow programmatically for all the list items.

How to start SharePoint list workflow using PowerShell automatically?
How to start the SharePoint list workflow using PowerShell automatically?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#The below script is used to start the workflow in SharePoint list items programmatically.
 
Add-PSSnapin Microsoft.Sharepoint.Powershell
cls
 
$fileName = "Workflow Start Report"
#'yyyyMMddhhmm yyyyMMdd
$enddate = (Get-Date).tostring("yyyyMMddhhmmss")
#$filename = $enddate + '_VMReport.doc'
$logFileName = $fileName +"_"+ $enddate+"_Log.txt"
$invocation = (Get-Variable MyInvocation).Value
$directoryPath = Split-Path $invocation.MyCommand.Path
 
$directoryPathForLog=$directoryPath+"\"+"LogFiles"
if(!(Test-Path -path $directoryPathForLog))
{
New-Item -ItemType directory -Path $directoryPathForLog
#Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
}
#$logPath = $directoryPath + "\" + $logFileName
 
$logPath = $directoryPathForLog + "\" + $logFileName
 
$isLogFileCreated = $False
 
function Write-Log([string]$logMsg)
{
if(!$isLogFileCreated){
Write-Host "Creating Log File..."
if(!(Test-Path -path $directoryPath))
{
Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
}
else
{
$script:isLogFileCreated = $True
Write-Host "Log File ($logFileName) Created..."
[string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
Add-Content -Path $logPath -Value $logMessage
}
}
else
{
[string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
Add-Content -Path $logPath -Value $logMessage
}
}
 
$web = Get-SPWeb "http://server:port"; #Your site name
$wfManager = $web.Site.WorkFlowManager;
$myList = $web.Lists["TestList"]; #Your list name
 
$association = $myList.WorkflowAssociations.GetAssociationByName("Test Workflow 2020","en-US"); #Pass your list name and culture string like en-US
 
$myView = $myList.Views["All Items"]; #Pass the view name like "All Items"
 
$listItems = $myList.GetItems($myView);
$associationData = $association.AssociationData;
 
foreach ($oneItem in $listItems)
{
try
{
$wf = $wfManager.StartWorkFlow($oneItem,$association,$associationData,$true);
 
$message="Workflow has been started on: "+$oneItem
Write-Host $message -BackgroundColor Green
}
catch
{
$ErrorMessage = $_.Exception.Message +"in starting the workflow on!: " +$oneItem
Write-Host $ErrorMessage -BackgroundColor Red
Write-Log $ErrorMessage
 
}
}
 
Write-host "The execution has been completed Successfully "

Summary:

Thus, in this PowerShell script – we have learned about how to start SharePoint workflow for all items in the list using the PowerShell script programmatically or how to start a workflow on multiple items on a list.




Share on Google Plus

About Tom DeMeulenaere

Highly accomplished information technology professional with extensive knowledge in System Center Configuration Manager, Windows Server, SharePoint, and Office 365.
    Blogger Comment

0 comments:

Post a Comment