Automating Document Approval Workflows using PnP PowerShell

Loading

Document approval workflows in SharePoint streamline content review before publishing. Using PnP PowerShell, you can automate document approval processes, ensuring compliance and efficiency.


Step 1: Install and Connect PnP PowerShell

Before executing any commands, install and connect PnP PowerShell:

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

Then, authenticate and connect to SharePoint Online:

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

Connected to SharePoint Online!


Step 2: Enable Content Approval on a Document Library

Ensure content approval is enabled for the document library:

$LibraryName = "Documents"
Set-PnPList -Identity $LibraryName -EnableModeration $true

This enables content approval, ensuring documents require approval before publishing.

Content approval enabled!


Step 3: Assign Approvers to the Library

Assign a group of users as document approvers:

$ApproverGroup = "Document Approvers"
Set-PnPList -Identity $LibraryName -ApprovePermissionLevels $ApproverGroup

Approvers assigned!


Step 4: Automate Document Submission for Approval

To submit a document for approval, use:

$FileUrl = "/sites/DocumentApproval/Shared Documents/Proposal.docx"
Set-PnPListItem -List $LibraryName -Identity 1 -Values @{"_ModerationStatus"= 1}

🔹 ModerationStatus Codes:

  • 0 → Approved
  • 1 → Pending
  • 2 → Rejected

Document submitted for approval!


Step 5: Approve or Reject Documents

Approvers can approve a document:

Set-PnPListItem -List $LibraryName -Identity 1 -Values @{"_ModerationStatus"= 0}

To reject a document:

powershellCopyEditSet-PnPListItem -List $LibraryName -Identity 1 -Values @{"_ModerationStatus"= 2}

Document approved/rejected!


Step 6: Automate Approval Workflow with Power Automate

You can trigger a Power Automate flow from PnP PowerShell:

$FlowUrl = "https://prod-XX.azure.com/workflows/your-flow-id/triggers/manual/run"
Invoke-RestMethod -Uri $FlowUrl -Method Post -Headers @{Authorization="Bearer your-access-token"}

Approval workflow triggered!


Step 7: Monitor Approval Status

To check approval status of all pending documents:

Get-PnPListItem -List $LibraryName | Select Title, FileRef, @{Name="Approval Status";Expression={$_.ModerationStatus}}

Approval statuses retrieved!

Leave a Reply

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