Saturday, October 8, 2016

SharePoint Online: How to Check in a Document using PowerShell CSOM

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 to check in a document
Here is the 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"
  
#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

The above script gets the file from URL. Alternatively, you can get file from Item ID and check in. Here is an another example.
#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://abraaj.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
    $File.CheckIn("Checked-in from PowerShell",[Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
    $Ctx.ExecuteQuery()
    
    write-host "Document has been checked-in successfully!" -f Green
}



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...