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

fig : Complete PowerShell script to download specific version of given document / file
fig : Complete PowerShell script to download specific version of given document / file

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
fig : SharePoint Online - error while opening version history of document
fig : SharePoint Online – error while opening version history of document
  • 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.
fig : SharePoint online - looking for "Version History" of given document
fig : SharePoint online – looking for “Version History” of given document
  • From there also we have an error
fig : SharePoint Online - error while opening version history from document
fig : SharePoint Online – error while opening version history from document

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
fig : SharePoint Online - respective version of document downloaded successfully
fig : SharePoint Online – respective version of document downloaded successfully

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"
}

fig : Complete PowerShell script to download specific version of given document / file
fig : Complete PowerShell script to download specific version of given document / file

References

Thanks for reading ! HAVE A FANTASTIC LEARNING AHEAD 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.