Export SharePoint List Item Version History to Excel

I wrote a PowerShell script and C# object model codes to generate SharePoint version history report in SharePoint document version history report, related to that, got a new requirement to export version history to excel.

This version history report helps to compare what data is changed, who changed the list data, etc over time.  just replace the values of WebURL, ListName variables with your Site/List values and run the script to export version history SharePoint 2013

# ******* Variables Section ******************
#Define these variables
$WebURL="http://sharepoint.constsco.com/sites/Sales/"
$ListName ="Invoice"
$ReportFile "D:\Invoice_VersionHistory.csv"
# *********************************************
#delete file if exists
If (Test-Path $ReportFile)
 {
 Remove-Item $ReportFile
 }
#Get the Web and List
$Web = Get-SPWeb $WebURL
$List $web.Lists.TryGetList($ListName)
 #Check if list exists
 if($List -ne $null)
 {
  #Get all list items
  $ItemsColl $List.Items
   
  #Write Report Header
  Add-Content -Path $ReportFile -Value "Item ID, Version Lable, Created by, Created at, Title"
  
  #Loop through each item
  foreach ($item in $ItemsColl)
  {
   #Iterate each version
      foreach($version in $item.Versions)
       {
    #Get the version content
    $VersionData "$($item.id), $($version.VersionLabel), $($version.CreatedBy.User.DisplayName), $($version.Created), $($version['Title'])"
    #Write to report
    Add-Content -Path $ReportFile -Value $VersionData
   }
  }
 }
Write-Host "Version history has been exported successfully!"

Please note, in Line number 25 and 36, I've added only "Title" field from the list. You may want to add additional fields as per your requirement. on running the script, all list item versions will be exported to excel (CSV format).

I

Comments

Popular posts from this blog

Clearing the Configuration Cache for SharePoint

SharePoint 2013 REST API CRUD Operations

Add List Item Attachments to Task Form using Nintex Workflow and Forms