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?
To check in a checked out file, follow these steps in SharePoint online.
  • 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, 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 Checking in a document but retain it to checkout, 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

SharePoint Online PowerShell CSOM to Check In a Document
#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("",[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 URL. Alternatively, you can get file from Item ID and check in. Here is an another PowerShell way of check in file 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
}

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

SharePoint Online: PnP PowerShell to Check In
#Config Variables
$SiteURL = "https://crescenttech.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 in version history page:
sharepoint online check in powershell
SharePoint Online: How to Check In a Document using PowerShell? SharePoint Online: How to Check In a Document using PowerShell? Reviewed by Salaudeen Rajack on October 08, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.