Get the Checkin Comments in SharePoint using PowerShell

Today’s post is not about PowerShell PnP, nor SharePoint Online, but it was such a struggle that I really wanted to share my experience, and my script.
Hopefully it will help you in case you’re in the same situation…

 

Scenario

The reason why I was trying to get those check-in comments was because I was preparing to get a report from an on-prem SharePoint 2013 document libraries with some information, so I could migrate files to SharePoint Online using PowerShell and of course, bring over all the versions & the check-in comments if any.

ℹ️ This script is not applicable to SharePoint Online, and was tested on SharePoint 2013/2016 only.

 

What was the struggle about?

I thought that running a usual foreach loop with some properties would do, and that’ll be it. But not.
And here starts a long week of searching, testing, searching again, and testing again 😩

I managed to get the files and their versions, but the check-in comments were all over the place!
Only the last comment was showing up (for the current version), or I had all the files 3 times in my CSV exported **sighs again**. I literally stared at my export and my script for hours trying to figured out what was the problem, and trying to get to the bottom of it.

So I went onto Twitter and got some input from Jake Encinas that pointed me in the right direction. Thanks again Jake!

 

The Script

So without further due, now that you know that the struggle was real, let’s get into it!
For this scenario, I was importing a csv file containing a list of multiple sites/subsites I had to get the information from. But it doesn’t really matter, you can adapt to your own situation 😉

Those are the most important things to remember:

  1. We have 3 foreach loops
    • Loop through the site URLs from the CSV file imported
    • Loop through all the items (files and folders) in each document libraries
    • Loop through each item (file) version(s)
  2. We use a condition (IF / ELSE statement) to check if the file is the current version of not.

❗There’s another “gotcha”, but I’ll tell you later 😉

 

#Add SharePoint snapin
Add-PSSnapin Microsoft.SharePoint.PowerShell

#Import the 'Statistics' file
$csvFile = Import-Csv -Path "C:\users\$env:USERNAME\Desktop\mySiteUrls.csv"
$results = @()

foreach ($site in $csvFile) {
    $spSubsite = Get-SPWeb -Identity $site.SiteURL
    $lists = $spSubsite.Lists | Where-Object { $_.BaseTemplate -eq "DocumentLibrary" }
    $allListItems = $lists.Items
    
    foreach ($item in $allListItems) {
        foreach ($Ver in $item.Versions) {
            
            #If the version is NOT the current one
            if ($Ver.FileVersion.IsCurrentVersion -eq $false) {
                $results += [pscustomobject][ordered]@{
                    FileType          = $item.FileSystemObjectType
                    SourceLibraryName = $item.ParentList
                    SourceSite        = $spSubsite.Url
                    FileOrFolderName  = $Ver["FileLeafRef"]
                    ID                = $item.ID
                    Created           = $Ver["Created"]
                    Modified          = $Ver["Modified"]
                    Version           = $Ver.VersionLabel ## Will give 1.0 or 2.0
                    CheckinComments   = $Ver.FileVersion.CheckInComment ## Will give the checkin comment from the version that is not current
                    "Created By"      = $item.File.Author.DisplayName
                    "Modified By"     = $Ver.FileVersion.CreatedBy.DisplayName
                }   
            } 
            
            # If the version IS the current one
            else {
                $results += [pscustomobject][ordered]@{
                    FileType          = $item.FileSystemObjectType
                    SourceLibraryName = $item.ParentList
                    SourceSite        = $spSubsite.Url
                    FileOrFolderName  = $Ver["FileLeafRef"]
                    ID                = $item.ID
                    Created           = $Ver["Created"]
                    Modified          = $Ver["Modified"]
                    Version           = $Ver.VersionLabel ## Will give 1.0 or 2.0
                    CheckinComments   = $item.File.CheckInComment ## Will give the latest checkin comment
                    "Created By"      = $item.File.Author.DisplayName
                    "Modified By"     = $item.File.ModifiedBy.DisplayName
                }   
            }
        }            
    }
}
$results | Export-Csv -Path "C:\users\$env:USERNAME\Desktop\SPOutput.csv" -NoTypeInformation

 

If you look closely at the CheckinComments (custom) object on both loops, they’re different. And that makes a BIG difference.
By using the $Ver.FileVersion.CheckInComment we are getting the comments from an “old” version (first loop), but using the $item.File.CheckInComment will give us the latest comment which is from the current version (second loop)!!

 

Someone mentioned a “gotcha“?

Indeed, I said that 😅

When checking if everything was OK, I noticed that the “Modified By” value was incorrect! Again, it was giving me the person whom last modified the file, but not the (different) ones for each version.

So pay attention to the $Ver.FileVersion.CreatedBy.DisplayName (first loop) and $item.File.ModifiedBy.DisplayName (second loop)
The script above will give you the correct values, and here I decided to go for the DisplayName but you can choose Email, or LoginName, or whatever else is available.

 

The Results

Here is the export! #HappyDays

 

 

 

 

Leave a Reply

%d bloggers like this: