SharePoint Online: Publish All Files in a Library using PowerShell
Requirement: Bulk Publish Multiple Files in SharePoint Online.
How to Publish Multiple Files in SharePoint Online?
To publish all documents in SharePoint Online, you can follow these steps:
- Go to your SharePoint document library, where the documents are stored.
- Select the documents that you want to publish by clicking the checkbox next to each document. You can also create a view by setting the Filter: Approval Status is equal to draft and set the view to show all files without Folders.
- From the command bar, select “Check in”.
- A pop-up window will appear, allowing you to check in with a comment explaining the changes you have made to the documents.
- Choose “Major version (Publish)” to publish all selected files and make them available to others.
PowerShell to Check in, Approve and Publish All Files in a Library
When you turn ON minor versioning in the SharePoint Online document library, any edits will not appear to users with read-only (or view-only) permissions until you publish them. Publishing each file individually in large libraries is definitely cumbersome, So here is my PowerShell script to publish all of them at once. Say, you want to publish all pages in the “Pages” library in SharePoint Online.
#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"
#Set Parameters
$SiteURL="https://Crescent.sharepoint.com"
$ListName ="Team Documents"
#Get Credentials to connect
$Cred= Get-Credential
Try{
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get All Items from the List
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$Ctx.Load($List)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
ForEach($ListItem in $ListItems)
{
#Approve the File if "Content Approval is Turned-ON"
If ($List.EnableModeration -eq $true)
{
If ($ListItem["_ModerationStatus"] -ne '0')
{
$ListItem.File.Approve("Approved by Admin")
Write-Host "File Approved: "$ListItem["FileLeafRef"] -ForegroundColor Yellow
$Ctx.ExecuteQuery()
}
}
#Checkin the File if its checked-out
If ($ListItem["CheckoutUser"] -ne $null)
{
$ListItem.File.CheckIn("Check-in by Admin", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
Write-Host "File Checked in: "$ListItem["FileLeafRef"] -ForegroundColor Cyan
$Ctx.ExecuteQuery()
}
#Publish the File
If($List.EnableVersioning -and $List.EnableMinorVersions)
{
$ListItem.File.Publish("Published by Admin")
$Ctx.ExecuteQuery()
Write-Host -f Green "File published:" $ListItem["FileLeafRef"]
}
}
}
Catch {
Write-host -f Red "Error:" $_.Exception.Message
}
PnP PowerShell to Publish All Files in a Document Library
You can use the PnP PowerShell to publish all documents in a SharePoint Online document library. The following is an example of how you can do this:
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Retail"
$LibraryName= "Invoices"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get All Files from the Document Library
$List = Get-PnPList -Identity $LibraryName
$ListItems = Get-PnPListItem -List $LibraryName -PageSize 2000 | Where { $_.FileSystemObjectType -eq "File" }
#Iterate through each file
ForEach ($Item in $ListItems)
{
#Get the File from List Item
$File = Get-PnPProperty -ClientObject $Item -Property File
#Approve the File if "Content Approval is Turned-ON"
If ($List.EnableModeration -eq $true)
{
If ($Item["_ModerationStatus"] -ne '0')
{
Set-PnPListItem -List $LibraryName -Identity $Item.ID -Values @{"_ModerationStatus"=0;"_ModerationComments"="Approved by Script"} | Out-Null
Write-host -f Green "Approved File:"$Item.FieldValues.FileRef
}
}
#Check in the file, if its checked out
If($File.CheckOutType -ne "None")
{
Set-PnPFileCheckedIn -Url $File.ServerRelativeUrl -CheckinType MajorCheckIn
Write-host -f Green "File Checked-In and Published:"$File.ServerRelativeUrl
}
Elseif($File.MinorVersion )# Check if file draft (Minor version)
{
$File.Publish("Major version Published by Script")
$File.Update()
Invoke-PnPQuery
Write-host -f Green "Published File at '$($File.ServerRelativeUrl)'"
}
}
BTW, the SPModerationStatusType enumeration has the below values:
- Approved – 0
- Denied -1
- Pending – 2
- Draft – 3
- Scheduled – 4