SharePoint Online: Manage Files Which Have No Checked in Version using PowerShell

When users don't provide value for mandatory metadata field values when uploading files (in classic experience) or when users forget to check in documents while the "Require Check Out" option under versioning settings of any library is set to "Yes", then those files are called "Files Which Have No Checked in Version", The file(s) which are uploaded will be stored on SharePoint but will be check out and visible only to the uploader! Often happens when users upload using File explorer view!

Manage Files Which Have No Checked In Version in SharePoint Online:
As a site administrator, You can take ownership of files which have no checked in version. To take control,
  • Go to "Manage files which have no checked in version" link under Library Settings, 
  • Select the documents and click on the "Take Ownership of Selection" button and confirmthe prompt. 
  • Once you took the ownership, the document gets removed from this page and starts appearing in the list where they are uploaded (only to You, until you check in them!) As a site owner, you can set the metadata properties if needed, check in and make these documents visible for all users . sharepoint online manage files which have no checked in version powershell
PowerShell to Take Ownership and Check-In All Files Which Have No Checked In Version 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 parameter values
$SiteURL="https://crescenttech.sharepoint.com/sites/Marketing/"
$ListName ="Documents"

Try{
    #Get Credentials to connect
    $Cred= Get-Credential
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Get Checked out Files with no checked in version
    $List = $Ctx.Web.lists.GetByTitle($ListName)
    $CheckedOutFiles = $List.GetCheckedOutFiles()
    $Ctx.Load($List)
    $Ctx.Load($CheckedOutFiles)
    $Ctx.ExecuteQuery()

    #Loop through each checked-out file
    ForEach($File in $CheckedOutFiles)
    {
        #Take Ownership of the File
        $CheckedOutByUser=$File.CheckedOutBy
        $Ctx.Load($CheckedOutByUser)
        $File.TakeOverCheckOut()
        $Ctx.ExecuteQuery()

        #Get the File and Check-in
        $CheckInFileURL = [String]::Concat($List.ParentWebUrl, $File.ServerRelativePath.DecodedUrl.Replace($List.ParentWebUrl, ""))
        $CheckInFile = $Ctx.web.GetFileByServerRelativeUrl($CheckInFileURL)
        $Ctx.Load($CheckInFile)
        $Ctx.ExecuteQuery()

        #Check in the file
        $CheckInFile.CheckIn("Checked-in from PowerShell",[Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
        $Ctx.ExecuteQuery()

        Write-Host -f Yellow "'$($CheckInFile.name)' at $($CheckInFile.ServerRelativeUrl) was Checked Out by: $($CheckedOutByUser.Email)"
        Write-host -f Green "Checked In The File Successfully!"         
    }
}
Catch {
        write-host -f Red "Error:" $_.Exception.Message
}
Can I take ownership & Check-in of all Files from a specific user? Sure:
#Get the User
$User = $Web.EnsureUser("[email protected]");

#Check if the File is checked out to the specific user
If($File.CheckedOutById -eq $User.Id)
{
 #Take ownership and check-in
}

PnP PowerShell to Check-in All Checkedout Files with No Checked-In Version 
This PowerShell script checks in all files checked out to a particular user.
#Set Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName = "Branding"
$UserID = "i:0#.f|membership|[email protected]"

#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -UseWebLogin
$Ctx = Get-PnPContext

#Get the List
$List = Get-PnPList $ListName
#Get the User
$CheckedoutByUser = Get-PnPUser -Identity $UserID

#Get All Checked-Out Files
$CheckedOutFiles = $List.GetCheckedOutFiles()
$Ctx.Load($CheckedOutFiles)
$Ctx.ExecuteQuery()

#Check-in All Files Checked out to the User
$CheckedOutFiles | ForEach-Object {
    If($_.CheckedOutById -eq $CheckedoutByUser.Id)
    {
        #Take over the Checked-Out File
        $_.TakeOverCheckOut()
        $Ctx.ExecuteQuery()

        #Check-in the file
        $CheckInFileURL = [String]::Concat($List.ParentWebUrl, $_.ServerRelativePath.DecodedUrl.Replace($List.ParentWebUrl, ""))
        Set-PnPFileCheckedIn -Url $CheckInFileURL -CheckinType MajorCheckIn -Comment "Checked-In by the Script at $(Get-Date)"    
        Write-Host "Check-in the File: $CheckInFileURL"    
    }
}

5 comments:

  1. Great article!
    But, we have a couple of questions.

    1) We're getting this error after running through the script once
    "Error Adding Custom Action! Exception calling "ExecuteQuery" with "0" argument(s): "File Not Found.""

    2) Where would we add the "Specific User" code?

    ReplyDelete
  2. Hello! Amazing article, but is there a way to do all this via sharepoint-pnp?

    ReplyDelete
    Replies
    1. Updated the article with PnP PowerShell script!

      Delete
  3. Hello,

    Tried using the PnP script. Worked well for small libraries but gives me the view threshold error for large document libraries.
    Is there a way to work around that?

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.