SharePoint Online: Publish All Files in a Library using PowerShell

Requirement: Bulk Publish Multiple Files in SharePoint Online

PowerShell to Check-in, Approve and Publish All Files in a Library:
When you turn ON minor versioning in 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 "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://crescenttech.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
}
You can also use "Content and Structure" to bulk publish documents in SharePoint Online!
SharePoint Online: Publish All Files in a Library using PowerShell SharePoint Online: Publish All Files in a Library using PowerShell Reviewed by Salaudeen Rajack on June 08, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.