How to start SharePoint list workflow using PowerShell automatically?

How to start SharePoint list workflow using PowerShell

No comments

 3,256 total views,  8 views today

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?

#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.

  • Learned about how to start the SharePoint list workflow using PowerShell.

You may also like the following SharePoint PowerShell tutorials: