SharePoint Online: How to Approve Multiple Documents using PowerShell?

Requirement: Approve Multiple Documents in SharePoint Online using PowerShell

How to Approve Multiple Items in SharePoint Online?

When the content approval is turned ON, each item must be approved in order to be visible to users. You can approve any item or document in SharePoint Online by: Right-click on the document >> Click on More >> Approve/Reject >> Approve

sharepoint online approve multiple documents

Approve a SharePoint Online Document using PowerShell:

Let’s approve a single file.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Parameters
$SiteURL = "https://crescenttech.sharepoint.com"
$FileRelativeURL ="/Shared Documents/Compliance Process.xlsx"
$ApprovalComments= "Approved by the Admin"

#Get credentials to connect
$Cred = Get-Credential

#Setup the Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName, $Cred.Password)
 
#Get the File and Approve
$File = $Ctx.Web.GetFileByServerRelativeUrl($FileRelativeURL)
$File.Approve($ApprovalComments)
$Ctx.ExecuteQuery()
 
Write-Host -f Green "File Approved Successfully!"

Similarly, to reject a file, use: $File.Deny($Comment). We can use the PowerShell script to approve multiple items in SharePoint Online.

SharePoint Online: Approve Multiple Documents using PowerShell

Let’s approve all pending documents in a document library.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Parameters
$SiteURL = "https://crescenttech.sharepoint.com"
$ListName ="Documents"

#Get credentials to connect
$Cred = Get-Credential

#Setup the Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName, $Cred.Password)
 
#Get the File and Approve
$List = $Ctx.web.Lists.GetByTitle($ListName)

#Query to get all items with "Pending" status
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "<View Scope = 'RecursiveAll'><Query><Where><Eq><FieldRef Name='_ModerationStatus' /><Value Type='ModStat'>2</Value></Eq></Where></Query></View>"
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()

#Approve all pending List Items
ForEach($Item in $ListItems)
{
    $Item["_ModerationStatus"] = 0
    $Item.Update()
    $Ctx.ExecuteQuery()
    Write-host -f Green "Approved List Item:" $Item.Id
} 

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply