Creating Documents Version History Report against SharePoint 2016 Document Library

Hello There,

I am writing down this particular article as I have faced strange issues despite of availability of many scripts over internet which works well mostly for SharePoint Online but not for SharePoint 2016 environment especially when you require to run it on remote server.

After accumulating the information from the net and resolving some issues with the help of PowerShell MVPs ( Chendrayan and Senthamil ) I was able to complete this task and I think this is really interesting and yeah, PowerShell has all the power to finish your work, just require the right direction and good understanding on the process.

In order to run this script on your machine, what you require is to downloading SharePoint 2016 client side components which you can get it from here.

Write down the following script in your PS file and run in windows powershell ( I have used PowerShell 5.0 )

# ******* Variables Section ******************
$loc = "D:\Scripts" # Location of DLL's
$loginname = "Your login name"
$siteUrl = "http://YourSharePointSiteURL"
$ListName ="DocumentLibraryName"
$ReportFile = "D:\Scripts\Documents_VersionHistory_Report.csv"
# ******* Variables Section ******************

#Authentication Process
Set-Location $loc
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.Runtime.dll")
Write-Host "Please enter password for $($siteUrl):"
$pwd = Read-Host -AsSecureString
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$ctx.Credentials = New-Object System.Net.NetworkCredential($loginname, $pwd)

#get web object
$web = $ctx.Web
$ctx.Load($web)
$ctx.ExecuteQuery()

#get list object
$list = $web.Lists.GetByTitle($ListName);
$ctx.Load($list)
$ctx.ExecuteQuery()
#delete file if exists
If (Test-Path $ReportFile)
{
Remove-Item $ReportFile
}

#Check if list exists
if($list -ne $null)
{
#Get all list items
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml = "<View Scope='Recursive'><Query><OrderBy><FieldRef Name='Modified' Ascending='FALSE' /></OrderBy></Query></View>"
$ListItems = $list.GetItems($query)
$ctx.Load($ListItems)
$ctx.ExecuteQuery()

#Write Report Header
Add-Content -Path $ReportFile -Value "Created at, Item Title, Version ID , Version Lable,Version Comment"

#Loop through each item
$ListItems | ForEach-Object {
$Item = $_
if($Item["Title"] -ne $null ) {

#load file object
$f = $Item.File
$ctx.Load($f)
$versions = $f.Versions
$ctx.Load($versions)
$ctx.ExecuteQuery()

#get the current version content
$currentVersion = "$($f.TimeCreated),$($Item['Title']),$($f.UIVersion ),$($f.UIVersionLabel),$($f.CheckInComment)"
Add-Content -Path $ReportFile -Value $currentVersion

foreach($version in $versions)
{

#Get the previous version content
$VersionData = "$($version.Created),$($Item['Title']),$($version.ID ),$($version.VersionLabel),$($version.CheckInComment)"

# Add-Content -Path $ReportFile -Value $VersionData
Add-Content -Path $ReportFile -Value $VersionData
}

}
}
}

#dispose web object
$web.Dispose()

Write-Host "Docuemnt Version history report has been exported successfully!"

You can download this script from the Microsoft Gallery as well.

Hope its helpful and do comment below if you are facing any issues or concerns.

Happy SharePointing !

Keep Running and Stay Young 🙂

Dipti Chhatrapati

Dipti Chhatrapati is a SharePoint specialist currently working as a Senior Consultant for CapGemini in Mumbai, India. She has more than 9 years of experience in application development, design, maintenance, administration and is continuously working with improving her skills and keeping them up to date. As a developer, project leader and SharePoint Business Analyst, she has been an extensive user of Visual Studio and Team Foundation Server throughout her career. Dipti is very passionate about music, sports, and arts and loves to spend her free time learning new things, socializing with friends and family, cooking, exercising and singing.

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz
Animated Social Media Icons by Acurax Wordpress Development Company
Visit Us On TwitterVisit Us On FacebookVisit Us On Linkedin