Tagged: PnP

SharePoint Online: Discard Check Out using PnP PowerShell

If you want to make changes to a file on a SharePoint Online site and want to make sure no one else can edit it, check out the file. When you have the file checked out, you can edit it online or offline, and save it—multiple times, if necessary.

When you finish editing and check the file back into the library, other people can see your changes and edit the file, if they have permission. And, if you decide not to make or keep any changes in the file, you can simply discard your checkout so you don’t affect version history.

You can discard check out individual document by right click on the document context menu, choose More –> Discard Check Out.

The Checked Out file can not be Edited/Removed by other users. It creates the issues when working with files. You can use PowerShell PnP Library to Discard File Check Out at ones:

 
#################################################################################################### 
# 
# Author.......: David Shvartsman 
# Date.........: 06/02/2022 
# Description..: SharePoint Online: Discard Check Out using PnP PowerShell 
# 
####################################################################################################
CLS
$SiteURL = "https://contoso.sharepoint.com/sites/site1"
$LibraryName ="Library1"

Write-host "Processing $($SiteURL) ..."
Connect-PnPOnline -Url $SiteURL -Credentials $credential
$List = Get-PnPList $LibraryName
# Get Context
$clientContext = Get-PnPContext
foreach ($File in Get-PnPListItem -List $LibraryName -PageSize 500) {
    if ($File.FieldValues.CheckoutUser -ne $null) {
        try {
            write-host "File $($File.FieldValues.FileRef) is Checked out to $($File.FieldValues.CheckoutUser.LookupValue). Performing UndoCheckOut ..." -NoNewline
            # -Url: relative path of the file
            # -AsListItem: Retrieves the file and returns it as a ListItem object
            $ListItem = Get-PnPFile -Url $File.FieldValues.FileRef -AsListItem
            # Get the File object
            $targetFile = $ListItem.File 
            $clientContext.Load($targetFile)
            $clientContext.ExecuteQuery()
            # The file should be checked out for this operation
            $targetFile.UndoCheckOut()
            $clientContext.ExecuteQuery()
            write-host "Done!" -ForegroundColor Green
        }
        catch {
            write-host "Error: $($Error[0])" -ForegroundColor Red
        }
    }
}
 
Disconnect-PnPOnline

###################################################################################################

That was another page in the Chronicles of SharePoint Bits, happy scripting!

SharePoint Online: Approve Pages in Pending Status using PnP PowerShell

When publishing is enabled in SharePoint Online, all the edits of the documents/pages will have the minor versions (or drafts). All changes to any document isn’t available to users with read only permissions, until its Published. Published documents becomes major versions such as 1.0, 2.0, 3.0, etc.

Document will have multiple statuses: Scheduled, Draft, Pending, and Approved.

You can publish the individual document by right click on the document to publish –> In the context menu, choose More –> and then click on Publish.

You can use PowerShell to Publish multiple documents at ones:

 
#################################################################################################### 
# 
# Author.......: David Shvartsman 
# Date.........: 05/07/2021 
# Description..: SharePoint Online: Approve Pages in Pending Status using PnP PowerShell 
# 
####################################################################################################
CLS
$SiteURL = "https://contoso.sharepoint.com/sites/site1"
$LibraryName ="Library1"

Write-host "Processing $($SiteURL) ..."
Connect-PnPOnline -Url $SiteURL -Credentials $credential -IgnoreSslErrors

#Get All Files from the document library - In batches of 500 and _ModerationStatus equel 2 (Approval Status: Pending)
$ListItems = Get-PnPListItem -List $LibraryName -PageSize 500 | Where {$_["_ModerationStatus"] -eq 2} 
$DocumentsData=@()
Write-host "Number of items found in '$($LibraryName)' $($ListItems.count)"
ForEach($Item in $ListItems)
{
    #Collect Documents Data
    $DocumentsData += New-Object PSObject -Property @{
        FileName = $Item.FieldValues['FileLeafRef']
        FileURL = $Item.FieldValues['FileRef']
        Status = $Item.FieldValues._ModerationStatus
    }
    Set-PnPFileCheckedIn -Url $Item.FieldValues['FileRef'] -CheckinType OverwriteCheckIn -Comment "Automated Approval" -Approve
}
$DocumentsData | Export-csv "C:\temp\PendingPages.csv"


Write-host "Processing $($SiteURL) completed."
Write-host ""
Write-host ""
Write-host ""
Write-host ""

###################################################################################################

The values of ‘_ModerationStatus’ property are:

0 – Approved

2 – Pending

3 – Draft

4 – Scheduled

You can use the script to select pages in different Approval Status and Approve and/or Publish those using Set-PnPFileCheckedIn PnP command.

That was another page in the Chronicles of SharePoint Bits, happy scripting!