For some reason, I stumbled on a problem that not all files were visible for all people. Disfact that the files we’re checked in, versioning was set ok, people should see all documents, check-in / check-out was enabled and approval of files was enabled.

I found that there were only minor version of those documents. No idea what happened (and I had the same problem at different customers with many affected files in one document library).

And as always PowerShell to the rescue!

I wrote a little script that first checks out all documents and after that checks them in again. Even found a way to preserve the approval status.


clear

$debug = $false

$startdate = Get-Date
Write-Host "Start time: $($startdate)"
write-Host "-------------------------------------------------------------------------------------------------------"
Write-Host

$user = "?????????????"
$password = ConvertTo-SecureString "?????????????" -AsPlainText -Force
$credentials = New-Object –TypeName "System.Management.Automation.PSCredential" –ArgumentList $user, $password

$siteURL = "https://tenant/sites/site"
$doclib = "Documents"

Connect-PnPOnline -Url $siteURL -Credentials $credentials
$ctx = Get-PNPContext

Write-Host "Connected to:" $siteURL -ForegroundColor Green
write-Host "-------------------------------------------------------------------------------------------------------" -ForegroundColor Green

$modStatHistory = @{}

$files = Get-PnPListItem $doclib
foreach($file in $files){
  $cow = Get-PnPListItem -id $file.Id -List $doclib

  Write-host "$($cow.FieldValues['_ModerationStatus']) - $($cow.FieldValues['FileRef'])"

  $modStatHistory[$file.Id] = $cow.FieldValues["_ModerationStatus"]

  if($debug -ne $true) { Set-PnPFileCheckedOut -Url $($cow.FieldValues['FileRef']) }
  write-host " - File checked out." -ForegroundColor Magenta -NoNewline
  if($debug) { write-host " DEBUG" -ForegroundColor Yellow } else { write-host }

  #0 The list item is approved. 
  #1 The list item has been denied approval. 
  #2 The list item is pending approval. 
  #3 The list item is in the draft or checked out state. 
  #4 The list item is scheduled for automatic approval at a future date. 

  #if($cow.FieldValues["_ModerationStatus"] -eq 3) { #checked out
  #  if($debug -ne $true) { Set-PnPFileCheckedIn -Url $($cow.FieldValues['FileRef']) -Comment "Updated by administrator" -CheckinType MinorCheckIn }
  #  write-host " - File minor checked in." -ForegroundColor Magenta -NoNewline
  #} else {
     if($debug -ne $true) { Set-PnPFileCheckedIn -Url $($cow.FieldValues['FileRef']) -Comment "Updated by administrator" }
     write-host " - File major checked in." -ForegroundColor Magenta -NoNewline
  #}
  if($debug) { write-host " DEBUG" -ForegroundColor Yellow } else { write-host }

  if($debug -ne $true) {
    $cow1 = Get-PnPListItem -id $file.Id -List $doclib
    $cow1["_ModerationStatus"] = $modStatHistory[$file.Id]
    $cow1.Update()
  }
  write-host " - File moderation status reset." -ForegroundColor Magenta -NoNewline
  if($debug) { write-host " DEBUG" -ForegroundColor Yellow } else { write-host }

  write-Host "-------------------------------------------------------------------------------------------------------"
}

write-host
Write-host "Checking for errors" -ForegroundColor Yellow
write-Host "-------------------------------------------------------------------------------------------------------" -ForegroundColor Yellow
$noErrors = $true
foreach($file in $files){
  $cow = Get-PnPListItem -id $file.id -List $doclib
  $modstat = $cow.FieldValues["_ModerationStatus"]
  $historyModStat = $modStatHistory[$file.id]

  if($modstat -ne $historyModStat) {
    $noErrors = $false
    write-host "C:$($modstat) - H:$($historyModStat) | $($cow.FieldValues['FileRef'])" -ForegroundColor Red
  }
}
if($noErrors) {
  write-host "No errors found" -ForegroundColor Green
}

Write-Host
write-Host "-------------------------------------------------------------------------------------------------------"
$enddate = Get-Date
Write-Host "End time: $($enddate)"

Write-Host
$timetext = "seconds"
$duration = (New-TimeSpan –Start $startdate –End $enddate).Seconds
if($duration -gt 60) {
  $duration = (New-TimeSpan –Start $startdate –End $enddate).Minutes
  $timetext = "minutes"
}
Write-Host "Duration: $($duration) $($timetext)"

if($debug) {
  Write-Host
  $estimate = ($duration + (($duration/100)*65))
  Write-Host "Estimate time real run: $($estimate) $($timetext)" -ForegroundColor Yellow
}

And yes, the script isn’t perfect, i.e. no error checks. And yes, the last modified date and modifier changes. That’s why it’s a quickfix.

Leave a Reply

Your email address will not be published. Required fields are marked *