4

I am using Office 365 SharePoint and have a requirement to require check in for all documents. As a result of this, whenever someone uploads a document, it is automatically checked out. Sometimes, the documents are never checked in. I want to write a script (through Powershell or CSOM - C#) that will check in files that have never been checked in.

I can manually do this by going to Library settings -> manage files with no checked in version, but I have a lot of document libraries and files. I have even encountered libraries that have over 5000 items that have never been checked in. There has to be a way to do this through code.

1

To check in all checked out files using PowerShell:

  • Open SharePoint Management Shell as Administrator.
  • Try to use this PowerShell code with your entries.

    $Web = Get-SPWeb http://yoursite/
    $libName = "LibraryName"
    $lib = $Web.Lists |? {$_.Title -eq $libName}
    foreach ($item in $lib.Items) 
    {
        $File = $item.File
        if( $File.CheckOutStatus -ne "None" -And $File.Versions.Count -eq 0)
        { 
            $File.CheckIn("CheckIn")
            Write-Host "$($File.Name) has been Checked In" -ForeGroundColor yellow
        }
    }
    $Web.Dispose()
    
  • This won't work for me. It gives me every file that isn't currently checked in. I need files that have been uploaded and never checked in and are not visible to others. – user1330008 Sep 7 '16 at 19:50
  • 1
    Please Check the updated answer, I add and condition when versions count equal 0 so it's never been checked in – Mohamed El-Qassas MVP Sep 7 '16 at 20:29
0

CSOM currently does not have the capability to manage files with no checked in version. In powershell or managed code, you can use the SPDocumentLibrary.CheckedOutFiles property to iterate through and force a checkin. This is the powershell script that I use:

#Force check-in of all documents
$webUrl = "https://sharepoint/SiteCollection"
$libraryTitle = "Shared Documents"

Try
{
    $web = Get-SPWeb $webUrl
    if($web -ne $null)
    {

    ForEach($list in ($web.Lists | ? {$_ -is [Microsoft.SharePoint.SPDocumentLibrary]}))
    {

        If($list.Title -eq $libraryTitle)
        {
            Write-Host "Evalating documents in " $list.Title
            # first get checked out files

            $fileIds = @()
            $Kount = 0
            Write-Host " Checking for files without a checked in version"
            ForEach($file in $list.CheckedOutFiles)
            {
                write-Host $file.LeafName
                $file.TakeOverCheckOut()  # to retrieve item i have to do this before
                $doc = $list.getItemById($file.ListitemId)
                $itemFile = $doc.File
                if($itemFile -ne $null){
                    $itemFile.CheckIn("Checked In By Administrator for achive")
                    write-Host " Checked In" -ForeGroundColor Green
                }
              $Kount ++
            }
            Write-Host " Found $Kount files that have no checked in version"
            Write-Host "  Evaluating files with checked in versions"

            ForEach($item in $list.Items)
            {
            Write-Host "   " $item.Folder "\" $item.File.Name
            if($item.File.CheckOutStatus -ne "None")
            {
                try
                {
                    $fileName = $item.File.Name
                    $userName = $item.File.CheckedOutByUser.Name;
                    Write-Host "Document $fileName is checked out by $userName" -f 
                    $item.File.CheckIn("CheckIn by Administrator")
                }
                catch
                {
                    Write-Host "Unable to check-in " $item.Url -ForegroundColor Red
                }
            }
            }
            $list.Update()
    }
}
}
}
Catch
{
     Write-Host ($Error[0].Exception)
}
0

I have tried to achieve this through csom powershell, but wasn't successful. Then i created a view like below screen shot and manually checked in the files.

Checked out to is greater than a

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.