1

I found this solution on this forum and was able to pull some information but with the workflow we have on the list, we need more information. We have an "Approval Status" column that we can see when it's updated when I look at the individual item's version history. Is there a way to modify this script to include it in the report? I tried adding it "Approval Status" to both places where I see the column names below but that did not work. TIA!

 # ******* Variables Section ******************
#Define these variables
$WebURL="http://sharepoint.crescent.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!"
1

You have enabled Content Approval in the list as it has "Approval Status".

The internal field name of "Approval Status" is "_ModerationStatus". You need to get its value by $version['_ModerationStatus']

Add-PSSnapin microsoft.sharepoint.powershell
# ******* Variables Section ******************
#Define these variables
$WebURL="http://sp/sites/team"
$ListName ="Invoice"
$ReportFile = "C:\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, Approval Status"

  #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']), $($version['_ModerationStatus'])"
    #Write to report
    Add-Content -Path $ReportFile -Value $VersionData
   }
  }
 }
Write-Host "Version history has been exported successfully!"

Testing result:

enter image description here

The "Approval Status" value: 0 is Approved; 1 is rejected 2 is Pending.

enter image description here

Or, you can use code to convert the value to friendly content:

Add-PSSnapin microsoft.sharepoint.powershell
# ******* Variables Section ******************
#Define these variables
$WebURL="http://sp/sites/team/"
$ListName ="Invoice"
$ReportFile = "C:\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, Approval Status"

  #Loop through each item
  foreach ($item in $ItemsColl)
  {
   #Iterate each version
      foreach($version in $item.Versions)
       {
       $status = switch($version['_ModerationStatus']){
        0 {"Approved"}
        1 {"Rejected"}
        2 {"Pending"}
        3 {"Draft"}
        4 {"Scheduled"}
       }
    #Get the version content
    $VersionData = "$($item.id), $($version.VersionLabel), $($version.CreatedBy.User.DisplayName), $($version.Created), $($version['Title']), $status"
    #Write to report
    Add-Content -Path $ReportFile -Value $VersionData
   }
  }
 }
Write-Host "Version history has been exported successfully!"

Testing result:

enter image description here

  • If I want to include information from another field, a custom field we added that is populated by a workflow, is that possible? I found that adding $($version.Field.FieldDisplayName) I don't get errors on the workflow anymore, but it does not pull the correct information. – MHodge Jan 22 at 16:05
  • What's the type of the field? Try using $version['<field internal name>']. How to get the field internal name: social.msdn.microsoft.com/Forums/office/en-US/… – JoannaW_MSFT Jan 23 at 1:21
  • It's a "person or group" field. Since "approving" the item shows the last person to modify the item was the approver, we wanted to make sure we captured separately the person who actually modified the item. I tried what you suggested, I also tried $version.editedby.user.displayname since that worked for "created by". That did not work either. – MHodge Jan 25 at 17:55
  • The "$version.CreatedBy.User.DisplayName" is the person who created this version, it also means the editor who edited the item. You can use it to get the person who actually modified the item. The "$version.Created" is the time when the editor edited the item and created the version. They don't mean who and when to created the item. – JoannaW_MSFT Jan 28 at 1:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.