SharePoint List Item Version History Retrieval
Summary
This script retrieves the version history of a specified list item, including all field values or only selected fields. The field values can be filtered by providing a comma-separated list of field names.
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$SiteUrl,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$ListName,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[int]$ItemId,
[string]$FieldNames = ""
)
Connect-PnPOnline -Url $SiteUrl -Interactive
$item = Get-PnPListItem -List $ListName -Id $ItemId
$file = Get-PnPProperty -ClientObject $item -Property File
$versions = Get-PnPProperty -ClientObject $item -Property Versions
$fileVersions = Get-PnPProperty -ClientObject $file -Property Versions
$listItemVersionHistory = @()
foreach ($version in $versions) {
$checkInComment = $fileVersions | Where-Object { $_.VersionLabel -eq $version.VersionLabel } | Select-Object -ExpandProperty CheckInComment
$fieldValues = $version.FieldValues
$fieldValuesFormatted = New-Object -TypeName PSObject
foreach ($field in $fieldValues.GetEnumerator()) {
$fieldName = $field.Key
$fieldValue = $field.Value
if ([string]::IsNullOrEmpty($FieldNames) -or ($FieldNames.Split(',') -contains $fieldName)) {
$fieldValuesFormatted | Add-Member -MemberType NoteProperty -Name $fieldName -Value $fieldValue
}
}
$listItemVersionHistory += [PSCustomObject]@{
VersionLabel = $version.VersionLabel
VersionId = $version.VersionId
IsCurrentVersion = $version.IsCurrentVersion
Created = $version.Created
CreatedBy = Get-PnPProperty -ClientObject $version.CreatedBy -Property Title
FieldValues = $fieldValuesFormatted | ConvertTo-Json -Compress
CheckInComment = $checkInComment
}
}
Write-Output $listItemVersionHistory
Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell
The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?
Contributors
Author(s) |
---|
Michał Romiszewski |
Kasper Larsen |
Disclaimer
THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.