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:

  • Navigate to the document library in your SharePoint or SharePoint Online site. Select the document which you would like to check out. 
  • Right-click on the File, choose the “Check Out” from the context menu. (or you can click on check out from the toolbar as well)
  • 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, instead of document URL, sometimes you may have to check out from the Item ID. Here is the script to checkout 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 PnP PowerShell 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 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