SharePoint Online: How to Check In a Document using PowerShell?
Check out-Check in feature in SharePoint avoids conflicts in typical collaboration environments, where multiple users may try to edit the same document at the same time. You must check in the document back in order to make the changes available to all other users, Even if the document is saved.
How to Check In a document in SharePoint Online?
Follow these steps in SharePoint Online to check in a checked-out file.
- Navigate to the document library where the checked out file is saved. Select the document(s) to Check in.
- From the ribbon, click the Files tab, and click on “Check In” from the “Open & Check Out” group.
- In the Check In dialog box, click No for “keep the file checked out after checking it in” option.
- 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.
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()
#check if the file 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:
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
Do you know if the document can be check in without change ‘Modified date’?. Thanks in advance.
Check-In Changes the Modified Date, Modified By Time stamp values! You have must change these metadata, refer: PowerShell to Update Created / Modified Values in SharePoint Online
How do I check in all documents in a site collection ?
Here you go: SharePoint Online: PowerShell to Bulk Check In All Documents