Microsoft 365 – PnP PowerShell – Getting/downloading version of document – resolving error related to version history – Sorry, something went wrong – looks like we cant show the version history for this file right now

Hi All,
Greetings for the day!!!
Today new issue and solution
Use Case / Background
- One of our SharePoint online user reported that he can not download the file from version history of one particular document in document library
- Even not able to see all the versions from UI – document library from SharePoint online site
- User is able to download document from other documents version history
- Issue is with one particular document – excel file only
- So the issue came to us – IT team and we solved it
Takeaway from the article – At the end of the article we got to know
- Prerequisites to connect SharePoint online using PnP PowerShell
- How to connect SharePoint online using PnP PowerShell
- How to read file (document) / get file object using PnP PowerShell
- How to get version history of file (document) using PnP PowerShell
- How to read the content of file using PowerShell
- How to download specific version of document / file using PnP PowerShell
Prerequisites
- PnP online PowerShell module is installed
- If PnP online PowerShell module is not installed then please download latest PowerShell module and install
- You could install PowerShell PnP module using Install-Module CMDLET
Install-Module PnP.PowerShell
Issue
- One of the user is not able to download specific version of document – excel file from document library
- Error while looking the version history
- Error – Pop Up appearing – Sorry, something went wrong as
- Next, we thought we will open the document / excel and from there we will look into versions – as one of the workaround – but no luck.
- From there also we have an error
Cause
- There is no specific cause found
- Actually the given document is migrated from the SharePoint on-prem environment
- And it seems while migrating document is corrupted
- So could open only recent version and not the any other version of the given document
- So solution we thought to download using PowerShell – so here is the article 🙂
Details / Solution – downloading specific file / document version using PnP PowerShell
- Next I thought to try with PowerShell
- So using PnP PowerShell – please look into Prerequisite section to use PnP PowerShell
- Connect to site using PnP PowerShell – using Connect-PnPOnline
#connect to PnP online - connect the site from which we need to delete the file
Connect-PnPOnline -Url https://knowledgejunction1.sharepoint.com/sites/Demo -Interactive
- Once we successfully connected to SharePoint online, next step is to get the respective document using – Get-PnPFile
#get respective file of which we need to download the version
$file = Get-PnPFile -Url "https://knowledgejunction1.sharepoint.com/sites/Demo/Shared%20Documents/coverpages.png"
Write-Host $file
- As we retrieve file object successfully, we have property – Versions and we will use Get-PnPProperty to get all versions of document / file
$fileVersions = Get-PnPProperty -ClientObject $file -Property Versions
$fileVersions
Output
Id VersionLabel Created
-- ------------ -------
512 1.0 3/22/2023 2:34:54 PM
1024 2.0 4/13/2023 11:26:52 PM
- Please note here – this dosent return current version of document / file
- Next step is to download the specific version – here we will download first version (1.0) having ID – 512
- Fetch the specific version. To fetch specific version we will use – GetById(<Version ID>) method
$version_1 = $fileVersions.GetById(512)
$version_1
- Next step is to set the path – where we need to download the file and with what name
#set the path where we need to download and give the specific name
$versionDocName = "C:\Prasham\Articles\PowerShell\SPO\DocumentVersionHistory\1.0_$($file.Name)"
- We need the context object to read the content of document / file
#here we need context
$pnpContext = Get-PnPContext
- As we successfully get context, next step is get the content of document / file
#fetch contents of the file version
$docVersionStream = $version_1.OpenBinaryStream()
$pnpContext.ExecuteQuery()
- And final step is download respective document / file
#download respective version of document
[System.IO.FileStream] $objFileStream = [System.IO.File]::Open($versionDocName,[System.IO.FileMode]::OpenOrCreate)
$docVersionStream.Value.CopyTo($objFileStream)
$objFileStream.Close()
- We could verify respective file on given path
Complete Script
try {
#connect to SharePoint online
Connect-PnPOnline -Url https://knowledgejunction1.sharepoint.com/sites/Demo -Interactive
#get respective file of which we need to download the version
$file = Get-PnPFile -Url "/sites/Demo/Shared Documents/coverpages.png"
$file
$fileVersions = Get-PnPProperty -ClientObject $file -Property Versions
$fileVersions
$version_1 = $fileVersions.GetById(512)
$version_1
#set the path where we need to download and give the specific name
$versionDocName = "C:\Prasham\Articles\PowerShell\SPO\DocumentVersionHistory\1.0_$($file.Name)"
#here we need context
$pnpContext = Get-PnPContext
#fetch contents of the file version
$docVersionStream = $version_1.OpenBinaryStream()
$pnpContext.ExecuteQuery()
#download respective version of document
[System.IO.FileStream] $objFileStream = [System.IO.File]::Open($versionDocName,[System.IO.FileMode]::OpenOrCreate)
$docVersionStream.Value.CopyTo($objFileStream)
$objFileStream.Close()
Write-Host -f Green "Version 1.0 Downloaded to :" $versionDocName
}
catch
{
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
$errorObj = New-Object PSObject -Property @{ Exceptions = "Error: $($_.Exception.Message)"}
#exporting errors to CSV file
$errorObj | Export-Csv -Append -Path
"C:\Prasham\Articles\PowerShell\SPO\DocumentVersionHistory\ErrorLogs.csv"
}
References
- PnP PowerShell overview – https://learn.microsoft.com/en-us/powershell/sharepoint/sharepoint-pnp/sharepoint-pnp-cmdlets
- Connect-PnPOnline – https://pnp.github.io/powershell/cmdlets/Connect-PnPOnline.html
- Microsoft 365 – Few approaches / options for Connecting to tenant using PnP PowerShell – Connect-PnPOnline – Part 1 – https://knowledge-junction.in/2021/12/16/microsoft-365-few-approaches-options-for-connecting-to-tenant-using-pnp-powershell-connect-pnponline-part-1/
- Microsoft 365 – Connecting to tenant using PnP PowerShell – Connect-PnPOnline – exploring DeviceLogIn (Device code flow) parameter- Part 2 – https://knowledge-junction.in/2021/12/18/microsoft-365-connecting-to-tenant-using-pnp-powershell-connect-pnponline-exploring-devicelogin-device-code-flow-parameter-part-2/
- Microsoft 365 : PowerShell – Connect-PnPOnline – resolving error – AADSTS50126: Error validating credentials due to invalid username or password – even though user name and password are correct – https://knowledge-junction.in/2023/04/03/microsoft-365-powershell-connect-pnponline-resolving-error-aadsts50126-error-validating-credentials-due-to-invalid-username-or-passwordmicrosoft-even-though-user-name-and-password-are-corre/
Thanks for reading ! HAVE A FANTASTIC LEARNING AHEAD 🙂
You must log in to post a comment.