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

Check out and Check in feature in SharePoint avoids conflicts in typical collaboration environments, where multiple users may try to edit the same document simultaneously. When you are finished making changes, You must check in the document back to make the changes available to all other users, Even if the document is saved. Checking in a document in SharePoint Online is a simple process and can be done in a few steps through web browser UI or PowerShell.

How to Check In a document in SharePoint Online?

Follow these steps in SharePoint Online to check in a checked-out file.

  1. Navigate to the document library where the checked out file is saved. Select the document(s) to Check in. 
  2. From the ribbon, click the Files tab, and click on “Check In” from the “Open & Check Out” group.
  3. In the Check In dialog box, click No for the “keep the file checked out after checking it in” option.
  4. Click OK.

If you check in a document but retain it to check out, other users get to see your latest changes while you may continue to work on the rest of the changes to your document.

how to checkin a document in sharepoint online powershell

If “Check In” is not an option, it means the document is not currently checked out. in Modern experience, You can select the checked out file >> Right-Click and choose “Check in” from the context menu or from the toolbar.

how to check in a document in sharepoint online

While this can be helpful, What if you want to automate this process? PowerShell is the way to go!

SharePoint Online PowerShell CSOM to Check In a Document

Let me show you how to check in a document in SharePoint Online using PowerShell:

#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 if file exists in given URL
Function Checkin-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 checked out
        If($File.CheckOutType -eq "None")
        {
            write-host "Document is not checked out Already!" -f Red
            break
        }
        #Checkin the document
        $File.CheckIn([string]::Empty,[Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
        #Checkin Types: MajorCheckIn,MinorCheckIn,OverwriteCheckIn

        $Ctx.ExecuteQuery()

        write-host "Document has been checked-in successfully!" -f Green
    }
    Catch {
        write-host -f Red "Error checking-in 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.xssn"

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

#Call the function to Checkin file
Checkin-Document -SiteURL $SiteURL -FileRelativeURL $FileRelativeURL -Credentials $Cred

SharePoint Online PowerShell to Check In file

The above script gets the file from the URL. Alternatively, you can get a file from Item ID and check in. Here is another PowerShell way of check in files 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 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

#Get the File to context
$Ctx.Load($File)
$Ctx.ExecuteQuery()
        
#determine if sharepoint document is checked out
If($File.CheckOutType -eq "None")
{
    write-host "Document is not checked out Already!" -f Red
}
else
{
    #Check in the file: sharepoint online force check in powershell 
    $File.CheckIn("Checked-in from PowerShell",[Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
    $Ctx.ExecuteQuery()
    
    write-host "Document has been checked-in successfully!" -f Green
}

SharePoint Online: PnP PowerShell to Check In a Document

PnP PowerShell makes it much simpler! Here is the PowerShell to check in a file 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)

#Check In a Document
Set-PnPFileCheckedIn -Url $FileRelativeURL -CheckinType MajorCheckIn -Comment "Checked In at $(Get-Date)" 

You can see the check-in result on the version history page:

sharepoint online check in powershell

Bulk Check in SharePoint Online Files using PowerShell:

To check in multiple files in SharePoint Online using PowerShell, use: SharePoint Online: PowerShell to Check In All Documents

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!

4 thoughts on “SharePoint Online: How to Check In a Document using PowerShell?

Leave a Reply

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