Get the check-in comments using PowerShell
Today’s post is not directly 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.
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.
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!
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:
- 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)
- We use a condition (
ELSEstatement) to check if the file is the current version of not.
❗There’s another “gotcha”, but I’ll tell you later 😉
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 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
LoginName, or whatever else is available.