SharePoint Online: PowerShell to Bulk Check In All Documents

Requirement: Bulk Check in All Documents in SharePoint Online using PowerShell

While checking in a single document is a matter of few clicks, We have a very large document library with lot of documents check out and want them to check in to make available to end users. Tried creating a view which filters all checked out files, but failed since the list item count is > 5000 (List view threshold issue!)
sharepoint online bulk check in powershell

SharePoint Online: PowerShell to Check in All Documents
Here is the PowerShell to check in multiple files in SharePoint Online document library.
#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://crescent.sharepoint.com/"
$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
}

SharePoint Online: Check in Multiple Files using PowerShell
Let's scan the entire site collection for checked out files and check in them back 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"
 
#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://crescent.sharepoint.com/"
 
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
#Call the function to Check-in All Files in a Site Collection
CheckIn-AllDocuments -SiteURL $SiteURL

If you need to check in a file in SharePoint Online use: PowerShell to Check In a Document in SharePoint Online
SharePoint Online: PowerShell to Bulk Check In All Documents SharePoint Online: PowerShell to Bulk Check In All Documents Reviewed by Salaudeen Rajack on July 08, 2017 Rating: 5

1 comment:

  1. I am getting the error when trying to check in multiple files in a document library in sharepoint online as below

    Error Check In Files! Exception calling ".ctor" with "2" argument(s): "The 'username' argument is invalid."

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.