How to resume older file version download in SharePoint online if interrupted/failed in between?

Mahesh Surkar 21 Reputation points
2022-11-10T06:34:51.807+00:00

While downloading large file older version in document library, if due any error (like network failure for second) if download (read() ) is failed or interrupted. In such scenario want to resume the download from that offset....

How to resume older file version download in SharePoint online if interrupted/failed in between?

Sample code to download older version of file is as below. Here changing offset does not work and file download starts from beginning.

           static void Main(string[] args)  
           {  
                string sitrUrl = "https://mysite.sharepoint.com/sites/test-site1";  
                //var fileURL = sitrUrl+ "/_vti_history/512/Shared Documents/txtDoc.txt";  
      
                using (var context = new ClientContext(sitrUrl))  
                {  
                      
                    context.Credentials = new SharePointOnlineCredentials("user1@xvz.com", StringToSecureString("password"));  
      
                    var targetSite = new Uri(context.Url);  
                    var webRequest = context.WebRequestExecutorFactory.CreateWebRequestExecutor(context, fileURL);  
      
                    String authenticationCookie = ((SharePointOnlineCredentials)context.Credentials).GetAuthenticationCookie(targetSite);  
                    webRequest.WebRequest.CookieContainer = new CookieContainer();  
                    webRequest.WebRequest.CookieContainer.Add(new Cookie("SPOIDCRL", authenticationCookie.TrimStart("SPOIDCRL=".ToCharArray()), String.Empty, targetSite.Authority));  
      
                    webRequest.Execute();  
      
                    var m_stream = webRequest.GetResponseStream();  
      
                    int offset = 0;  
                    int count = 25;  
      
                    while (true)  
                    {  
                        byte[] buffer = new byte[256];  
                        int bytesRead = m_stream.Read(buffer, offset, count);  
                        offset += bytesRead;  
                        //System.Console.WriteLine("offset:" + offset + ", bytesRead:" + bytesRead);  
                        //System.Console.WriteLine(Encoding.Default.GetString(buffer));  
                        if (bytesRead == 0)  
                            break;  
                    }  
                }  
            }  

@Tong Zhang_MSFT

@RaytheonXie_MSFT

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,567 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,938 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,604 questions
{count} votes

Accepted answer
  1. Tong Zhang_MSFT 9,146 Reputation points
    2022-11-11T06:30:26.877+00:00

    Hi @Mahesh Surkar ,

    According to my research and testing, please try to use the following code to download file with resume support:

    var buffer = new byte[4096];  
    int bytesRead;  
      
    while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)  
    {  
           totalBytesRead += bytesRead;  
           RequestContentLength += bytesRead;  
           localFileStream.Write(buffer, 0, bytesRead);  
    }  
    

    And here is a document about C# download file with resume, you can refer to the code in this document: C# download file with resume

    Hope it can help you. Thanks for your understanding.

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    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.


    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Romain LOPES 1 Reputation point
    2023-01-10T14:53:52.11+00:00

    did you find a way to keep your cookie container up to date with legacy authentication disabled ?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.