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 Adding Custom Action!" $_.Exception.Message
}
Can I take ownership & Check-in of all Files from a specific user? Sure:
#Get the User
$User = $Web.EnsureUser("Salaudeen@crescent.com");

#Check if the File is checked out to the specific user
If($File.CheckedOutById -eq $User.Id)
{
 #Take ownership and check-in
}
SharePoint Online: Manage Files Which Have No Checked in Version using PowerShell SharePoint Online: Manage Files Which Have No Checked in Version using PowerShell Reviewed by Salaudeen Rajack on August 11, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.