-
-
Save reshmee011/e65dfb211e2a9ba8a1044826f267e250 to your computer and use it in GitHub Desktop.
restore_sharepoint_list_previous_version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # array to save items that have been restored | |
| $ItemsRestored = [System.Collections.ArrayList]@(); | |
| function Restore-PreviousVersions { | |
| Param( | |
| [Parameter(Mandatory = $true)][string] $List, | |
| [Parameter(Mandatory = $true)][string] $Key, | |
| [Parameter(Mandatory = $true)][string] $User, | |
| [Parameter(Mandatory = $true)][string] $Date | |
| ) | |
| # Ensure the columns referenced in the CAML query are indexed, otherwise you will get the listview threshold error if there are more than 5000 items in the list. | |
| $query = @" | |
| <View> | |
| <Query> | |
| <Where> | |
| <Eq><FieldRef Name='Key' /><Value Type='Text'>$Key</Value></Eq> | |
| </Where> | |
| </Query> | |
| <ViewFields> | |
| <FieldRef Name='Id' /> | |
| </ViewFields> | |
| </View> | |
| "@ | |
| $counter = 0 | |
| $items = Get-PnPListItem -List $List -Query $query | Select-Object Id, ObjectVersion | |
| $items | ForEach-Object { | |
| if ($_.ObjectVersion -ne 1) { | |
| $listVersions = Get-PnPListItemVersion -List $List -Identity $_.Id | |
| $itemId = $_.Id | |
| if ($listVersions) { | |
| if ($listVersions.Count -gt 1) { | |
| $listVersions | ForEach-Object { | |
| if ($_.Values.Editor.LookupValue -eq $User -and $_.Created.ToString("yyyy-MM-dd") -eq $Date) { | |
| $informationMessage = "Edited by $($_.Values.Editor.LookupValue) on list $List with ID $itemId on $($_.Created.ToString("yyyy-MM-dd")) with version $($_.VersionLabel)" | |
| Write-Host $informationMessage -ForegroundColor Yellow | |
| Restore-PnPListItemVersion -List $List -Identity $itemId -Version ($_.VersionLabel - 1).ToString(".0") -Force | |
| $informationMessage = "Restored item $itemId to version $(($_.VersionLabel - 1).ToString(".0")) for filter $Key." | |
| Write-Host $informationMessage -ForegroundColor Yellow | |
| $ItemsRestored.Add([PSCustomObject]@{ | |
| ItemId = $itemId | |
| List = $List | |
| RestoredVersion = ($_.VersionLabel - 1).ToString(".0") | |
| FilterKey = $Key | |
| }) | |
| $counter += 1 | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| Write-Host "Total items updated on $List - $counter for $Key" -ForegroundColor Yellow | |
| } | |
| Start-Transcript | |
| # Generate a unique log file name using today's date and time | |
| $todayDate = Get-Date -Format "yyyy-MM-dd_HHmm" | |
| $restoredVersion = "restored-file-versions_$todayDate.csv" | |
| $restoredVersionFilePath = Join-Path -Path $PSScriptRoot -ChildPath $restoredVersion | |
| # Tenant URL | |
| $siteUrl = Read-Host -Prompt "Enter site collection URL" | |
| Connect-PnPOnline -Url $siteUrl -Interactive | |
| $PaymentsList = "Payments" | |
| Restore-PreviousVersions -List $PaymentsList -Key "202407-Ms-Software" -User "System Account" -Date "2024-07-13" | |
| $ItemsRestored | Export-Csv -Path $restoredVersionFilePath -NoTypeInformation -Force -Delimiter "," | |
| Stop-Transcript |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment