SharePoint Online: How to Check Out a Document using PowerShell?

Checking out a document in SharePoint ensures that you are the only one making edits to it. The purpose of checkout is to prevent conflicts in a collaborative environment where multiple people may try to edit the same document or list items simultaneously. So, When a file is checked out to you, you are the only one allowed to make changes to it, and the file will remain locked until you release it.

When you’re finished making edits, check in the document so that other users can view the changes you made. The “Require checkout” option can be turned ON or OFF from document library settings. This blog post will look at how you can check out a document using PowerShell in SharePoint Online.

How to Check Out a Document in SharePoint Online?

To check out documents in SharePoint Online, follow these steps:

  1. Navigate to the document library in your SharePoint or SharePoint Online site. Select the document which you would like to check out. 
  2. Right-click on the File, and choose “Check Out” from the context menu. (or you can click on check out from the toolbar as well)
  3. Once the document is checked out, you’ll get a tiny outward-pointing green arrow, in the document icon. how to checkout a file in sharepoint online powershell

PowerShell script to Check out a document in SharePoint Online

Use this PowerShell to check out a 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"
  
#Function to Check out a file from given URL
Function Checkout-Document($SiteURL, $FileRelativeURL, $Credentials)
{

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    Try {
        #Try to get the File from URL
        $File = $Ctx.web.GetFileByServerRelativeUrl($FileRelativeURL)
        $Ctx.Load($File)
        $Ctx.ExecuteQuery()
        
        #check if the file is already checked out
        If($File.CheckOutType -ne "None")
        {
            write-host "File is already checked-out!" -f Yellow
            break
        }
        #Checkout the document
        $File.CheckOut()
        $Ctx.ExecuteQuery()

        write-host "Document has been checked-out successfully!" -f Green
    }
    Catch {
        write-host -f Red "Error checking out Document!" + $_.Exception.Message
    }    
}

#Set Variables for Site URL, List Name and Column Name
$SiteURL= "https://crescent.sharepoint.com/sites/sales/"
$FileRelativeURL="/sites/Sales/Shared%20Documents/Legal%20Template.xsn"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#Call the function to Checkout file
Checkout-Document -SiteURL $SiteURL -FileRelativeURL $FileRelativeURL -Credentials $Cred 
Discard Checkout in SharePoint Online using PowerShell:
If you want to discard checkout, use: $File.UndoCheckOut() method.

SharePoint Online: Checkout Document using PowerShell CSOM

Alternatively, you may have to check out from the Item ID instead of the document URL. Here is the script to check out a document from the item ID:

#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 Variables for Site URL, Library Name and Item ID
$SiteURL= "https://crescent.sharepoint.com/sites/sales/"
$LibraryName="Documents"
$ItemID="4"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred

#Get the web, List, Item adn File
$Web=$Ctx.Web
$List=$web.Lists.GetByTitle($LibraryName)
$Item=$list.GetItemById($ItemID)
$File=$Item.File

#Check out the file
$File.CheckOut()
$Ctx.ExecuteQuery()

Check Out a File using PnP PowerShell

Here is how you can check out a document using the PnP PowerShell cmdlet Set-PnPFileCheckedOut in SharePoint Online:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$FileRelativeURL ="/Team Documents/Project Proposal.docx"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Checkout the File
Set-PnPFileCheckedOut -Url $FileRelativeURL

To check in a file using PowerShell, use: How to Check In a Document in SharePoint Online using PowerShell?

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

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