top of page

Script to check in files at SharePoint online

  • Sep 27, 2020
  • 3 min read



The below script can come handy while checking in specific files, all checked out files within library and in site by iterating the query to all libraries looking for checked out files and checking them in.


Before we begin with PowerShell scripts, Its important to install latest SharePoint client side components SDK assemblies from below link as it is a prerequisite to run the below mentioned scripts.



Let's check all scenarios one by one.


Script to check in a specific file located in a library at a SharePoint online:


Note: Modify the variables highlighted in script to check in required 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 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

$FileRelativeURL="/sites/newsite/Shared%20Documents/document.docx"

#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

Script to check in all checked out files within a specific library in a SharePoint online site:


Below script will check in all files in library that are checked out by some user in your organization.


Note: Modify the highlighted variables as required before running script.

#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 parameter values

$SiteURL="https://footballlife.sharepoint.com/sites/Classico"

$LibraryName="Documents"


Try{

#Get Credentials to connect

$Cred= Get-Credential

$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Setup the context

$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)

$Ctx.Credentials = $Credentials

#Get the Web

$Web = $Ctx.Web

$Ctx.Load($Web)

$Ctx.ExecuteQuery()

#Get the list

$List = $Web.Lists.GetByTitle($LibraryName)

#Prepare the query

$Query = New-Object Microsoft.SharePoint.Client.CamlQuery

$Query.ViewXml = "@

<View Scope='RecursiveAll'>

<Query>

<Where>

<IsNotNull><FieldRef Name='CheckoutUser' /></IsNotNull>

</Where>

</Query>

<RowLimit Paged='TRUE'>2000</RowLimit>

</View>"

#Batch Process items: SharePoint online PowerShell bulk check in

Do {

$ListItems = $List.GetItems($Query)

$Ctx.Load($ListItems)

$Ctx.ExecuteQuery()

$Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition

#Get All Checked out files

ForEach($Item in $ListItems)

{

#Get the Checked out File data

$File = $Ctx.Web.GetFileByServerRelativeUrl($Item["FileRef"])

$Ctx.Load($File)

$CheckedOutByUser = $File.CheckedOutByUser

$Ctx.Load($CheckedOutByUser)

$Ctx.ExecuteQuery()

Write-Host -f Yellow "Found a Checked out File '$($File.Name)' at $($Web.url)$($Item['FileRef']), Checked Out By: $($CheckedOutByUser.LoginName)"

#Check in the document

$File.CheckIn("Checked-in By Administrator through PowerShell!", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)

$Ctx.ExecuteQuery()

Write-Host -f Green "File '$($File.Name)' Checked-In Successfully!"

}

}

While($Query.ListItemCollectionPosition -ne $Null)

}

Catch {

write-host -f Red "Error Check In Files!" $_.Exception.Message

}


Script to check in all checked out files from all existing libraries in SharePoint online site:


Below script iterates to all libraries looking for checked out file and check it in thus making bulk check in of files and reducing the admins task when it comes to managing SharePoint Online tenant.

#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"

#PowerShell to Bulk check in all documents

Function CheckIn-AllDocuments([String]$SiteURL)

{

Try{

#Setup the context

$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)

$Ctx.Credentials = $Credentials

#Get the Web

$Web = $Ctx.Web

$Ctx.Load($Web)

$Ctx.Load($Web.Webs)

$Ctx.ExecuteQuery()

#Get All Lists from the web

$Lists = $Web.Lists

$Ctx.Load($Lists)

$Ctx.ExecuteQuery()

#Prepare the CAML query

$Query = New-Object Microsoft.SharePoint.Client.CamlQuery

$Query.ViewXml = "@<View Scope='RecursiveAll'>

<Query>

<Where>

<IsNotNull><FieldRef Name='CheckoutUser' /></IsNotNull>

</Where>

</Query>

<RowLimit Paged='TRUE'>2000</RowLimit>

</View>"

#Array to hold Checked out files

$CheckedOutFiles = @()

Write-host -f Yellow "Processing Web:"$Web.Url

#Iterate through each document library on the web

ForEach($List in ($Lists | Where-Object {$_.BaseTemplate -eq 101 -and $_.Hidden -eq $False}) )

{

Write-host -f Yellow "`t Processing Document Library:"$List.Title

#Batch Process List items

Do

{

$ListItems = $List.GetItems($Query)

$Ctx.Load($ListItems)

$Ctx.ExecuteQuery()

$Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition

#Get All Checked out files

ForEach($Item in $ListItems)

{

#Get the Checked out File data

$File = $Web.GetFileByServerRelativeUrl($Item["FileRef"])

$Ctx.Load($File)

$CheckedOutByUser = $File.CheckedOutByUser

$Ctx.Load($CheckedOutByUser)

$Ctx.ExecuteQuery()

Write-Host -f Green "`t`t Found a Checked out File '$($File.Name)' at $($Web.url)$($Item['FileRef']), Checked Out By: $($CheckedOutByUser.LoginName)"

#Check in the document

$File.CheckIn("Checked-in By Administrator through PowerShell!", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)

$Ctx.ExecuteQuery()

Write-Host -f Green "`t`t File '$($File.Name)' Checked-In Successfully!"

}

}While($Query.ListItemCollectionPosition -ne $Null)

}

#Iterate through each subsite of the current web and call the function recursively

ForEach($Subweb in $Web.Webs)

{

#Call the function recursively to process all subsites underneaththe current web

CheckIn-AllDocuments -SiteURL $Subweb.URL

}

}

Catch

{

write-host -f Red "Error Check In Files!" $_.Exception.Message

}

}

#Config Parameters

$SiteURL="https://footballlife.sharepoint.com/sites/Classico"

#Setup Credentials to connect

$Cred= Get-Credential

$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Call the function: sharepoint online PowerShell to check in all documents in a Site Collection

CheckIn-AllDocuments -SiteURL $SiteURL


Thank you for visiting the blog, Please do share your feedback if you liked it so that I can come up with more such blogs for community.


Because, "Sharing is Caring...!!"


 
 
 

Comments


bottom of page