Creating Custom Workflows using PnP PowerShell

Loading

Workflows in SharePoint Online automate business processes like approvals, notifications, and data processing. With PnP PowerShell, you can manage and deploy workflows using Microsoft Power Automate (Flow) or legacy SharePoint 2010/2013 workflows.

This guide will explain step-by-step how to create a custom approval workflow using PnP PowerShell.


Step 1: Install and Connect to SharePoint Online

Before running any PnP PowerShell commands, ensure you have PnP PowerShell installed:

Install-Module -Name PnP.PowerShell -Scope CurrentUser -Force

Then, connect to your SharePoint site:

$SiteUrl = "https://yourtenant.sharepoint.com/sites/yoursite"
Connect-PnPOnline -Url $SiteUrl -Interactive

🔹 Replace yourtenant with your tenant name
🔹 Replace yoursite with your SharePoint site name

Connected to SharePoint successfully!


Step 2: Create a List for the Workflow

If you don’t already have a list for the workflow, create one:

$ListName = "ApprovalRequests"
New-PnPList -Title $ListName -Template GenericList -Url "Lists/ApprovalRequests"

Approval Requests list created!


Step 3: Add Necessary Columns to the List

For an approval process, we need these columns:
Requester (Person)
Approval Status (Choice: Pending, Approved, Rejected)
Approver Comments (Multiple lines of text)

# Add "Requester" column (Person field)
Add-PnPField -List $ListName -DisplayName "Requester" -InternalName "Requester" -Type User

# Add "Approval Status" column (Choice field)
Add-PnPField -List $ListName -DisplayName "Approval Status" -InternalName "ApprovalStatus" -Type Choice -Choices "Pending", "Approved", "Rejected"

# Add "Approver Comments" column (Multiple lines of text)
Add-PnPField -List $ListName -DisplayName "Approver Comments" -InternalName "ApproverComments" -Type Note

List fields added!


Step 4: Create a Power Automate Flow (Modern Workflows)

Instead of using SharePoint Designer workflows, you can trigger Power Automate Flows using PnP PowerShell.

$FlowName = "Approval Workflow"
$FlowTemplateId = "b7beed32-50da-43bd-8b51-e69a3e2a1a9c" # Template ID for approval workflows
$FlowParams = @{
displayName = $FlowName
definition = @{
actions = @{
"Send_Approval_Email" = @{
"type" = "SendEmail"
"inputs" = @{
"To" = "@{triggerBody()?['ApproverEmail']}"
"Subject" = "New Approval Request"
"Body" = "You have a new approval request."
}
}
}
}
}

New-PnPFlow -DisplayName $FlowName -Definition $FlowParams

Power Automate Flow created!


Step 5: Attach Workflow to the SharePoint List

Now, let’s trigger the workflow when a new item is added:

$TriggerCondition = "@equals(triggerBody()?['ApprovalStatus'], 'Pending')"

Set-PnPFlowTrigger -List $ListName -TriggerType "Created" -FlowName $FlowName -TriggerCondition $TriggerCondition

Workflow attached to the list!


Step 6: Manually Starting a Workflow on an Item

If you want to manually trigger the workflow, use:

$ItemId = 1  # Replace with actual list item ID
Invoke-PnPFlow -FlowName $FlowName -List $ListName -ItemId $ItemId

Workflow started on item ID 1!


Step 7: Automate Workflow Execution with Scheduled Tasks

To run the workflow daily, schedule a PowerShell script:

1️⃣ Save this script as StartWorkflow.ps1:

$SiteUrl = "https://yourtenant.sharepoint.com/sites/yoursite"
Connect-PnPOnline -Url $SiteUrl -Interactive
$FlowName = "Approval Workflow"
$ListName = "ApprovalRequests"
$Items = Get-PnPListItem -List $ListName | Where-Object { $_["ApprovalStatus"] -eq "Pending" }

foreach ($Item in $Items) {
Invoke-PnPFlow -FlowName $FlowName -List $ListName -ItemId $Item.Id
}
Write-Host "All pending approvals processed!"

2️⃣ Schedule the script using Task Scheduler.
3️⃣ Set it to run every 24 hours.

Workflow automation completed!


Step 8: Monitoring Workflow Execution

To check workflow execution logs:

Get-PnPFlowRun -FlowName "Approval Workflow"

Workflow logs retrieved!


Step 9: Managing Workflows in SharePoint

List all Workflows

Get-PnPFlow

Delete a Workflow

Remove-PnPFlow -FlowName "Approval Workflow"

Disable a Workflow

Set-PnPFlowState -FlowName "Approval Workflow" -State Disabled

Workflow management simplified!

Leave a Reply

Your email address will not be published. Required fields are marked *