How to download versions of a file in SharePoint site

Naveen Kumar B 0 Reputation points
2024-06-26T06:41:56.5233333+00:00

https://learn.microsoft.com/en-us/answers/questions/1083204/how-to-resume-older-file-version-download-in-share

The above link has a way to download the file versions but as the basic authentication is disabled, it is not possible to use "SPOIDCRL" cookie to get authenticated for the below URL:
"https://mysite.sharepoint.com/sites/test-site1/_vti_history/512/Shared%20Documents/txtDoc.txt"

Is there any other ways to download it.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,105 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,773 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 33,176 Reputation points Microsoft Vendor
    2024-06-26T09:23:56.5+00:00

    Hi @Naveen Kumar B,

    I would recommend you to use csom powershell to download different versions of file. Please refer to following script

    #Load SharePoint CSOM Assemblies
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
       
    #Parameters
    $SiteURL = "https://Crescent.sharepoint.com"
    $FileRelativeURL ="/Shared Documents/Discloser China.doc"
    $DownloadPath= "C:\Temp"
     
    #Get credentials to connect
    $Cred = Get-Credential
     
    #Setup the Context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName, $Cred.Password)
      
    #Get the File and its versions
    $File = $Ctx.Web.GetFileByServerRelativeUrl($FileRelativeURL)
    $Ctx.Load($File)
    $Ctx.Load($File.Versions)
    $Ctx.ExecuteQuery()
      
    If($File.Versions.Count -gt 0)
    {
        Foreach($Version in $File.Versions)
        {
            #Frame File Name for the Version
            $VersionFileName = "$($DownloadPath)\$($Version.VersionLabel)_$($File.Name)"
             
            #Get Contents of the File Version
            $VersionStream = $Version.OpenBinaryStream()
            $Ctx.ExecuteQuery()
     
            #Create File version at local disk
            [System.IO.FileStream] $FileStream = [System.IO.File]::Open($VersionFileName,[System.IO.FileMode]::OpenOrCreate)
            $VersionStream.Value.CopyTo($FileStream)
            $FileStream.Close()
             
            Write-Host -f Green "Version $($Version.VersionLabel) Downloaded to :" $VersionFileName
        }
    }
    Else
    {
        Write-host "No Versions Available for "$Item["FileRef"] -f Yellow
    }
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.