Download older version of a file from SharePoint Document Library using CSOM RRS feed

  • Question

  • Download older version of a file from SharePoint Document Library using CSOM in mvc c#

    Tuesday, January 3, 2017 5:52 AM

All replies

  • Hi Jasia,

    You can access versions of a document via SharePoint's version history web service.

    Use the URL to the version you want to view. For example: http://yoursite/yoursubsite/_vti_history/512/DocLib/Book1.xls in the left window, and... http://yoursite/yoursubsite/_vti_history/1024/DocLib/Book1.xls in the right window will compare versions 1.0 and 2.0 of the same document item.

    With the version number in hand (version.VersionLabel in your code), you can generate the proper URL using the following formula: Major Version * 512 + Minor Version

    System.Net.WebClient client = new System.Net.WebClient();
    client ->Credentials = new NetworkCredential(UserNameText, PasswordText); 
    System.IO.Stream Data = client .OpenRead(url);

    Check below URL for more detail

    Please mark the "replies as answers" and "Vote as Helpful" if you find it helpful. |

    Tuesday, January 3, 2017 7:11 AM
  • code gives "The remote server returned an error: (403) Forbidden"  error on
    System.IO.Stream Data = client .OpenRead(url);
    Tuesday, January 3, 2017 9:48 AM
  • Hi,

    Try below code,

    Uri filename = new Uri(@"http://server/sites/site1/subsite/doclib/folder1/folder2/prettyimage.jpg");
    string server = filename.AbsoluteUri.Replace(filename.AbsolutePath, "");
    string serverrelative = filename.AbsolutePath;
    Microsoft.SharePoint.Client.ClientContext clientContext = new Microsoft.SharePoint.Client.ClientContext(server);
    Microsoft.SharePoint.Client.FileInformation f = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, serverrelative);
    using (var fileStream = new FileStream(@"d:\prettyimage.jpg", FileMode.Create))

    The Stream.CopyTo() method is an extension method.

    public static class StreamExtensions
        public static void CopyTo(this System.IO.Stream src, System.IO.Stream dest)
            if (src == null)
                throw new System.ArgumentNullException("src");
            if (dest == null)
                throw new System.ArgumentNullException("dest");
            System.Diagnostics.Debug.Assert(src.CanRead, "src.CanRead");
            System.Diagnostics.Debug.Assert(dest.CanWrite, "dest.CanWrite");
            int readCount;
            var buffer = new byte[8192];
            while ((readCount = src.Read(buffer, 0, buffer.Length)) != 0)
                dest.Write(buffer, 0, readCount);

    Check below URL for more detail

    Please mark the "replies as answers" and "Vote as Helpful" if you find it helpful. |

    Tuesday, January 3, 2017 1:15 PM
  • Error  "The remote server returned an error: (404) Not Found."  on

    Microsoft.SharePoint.Client.FileInformation f = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, serverrelative)

    as relative path points to /_vti_history.  if i try to access current version with same code its runs smoothly but when i am trying to access older version error arises.

    Please Help

    Wednesday, January 4, 2017 5:33 AM
  • Hi jasia,

    Here is a demo about downloading older file version for your reference:

     using (ClientContext clientContext = new ClientContext("http://sp/sites/test"))
                    Web web = clientContext.Web;
                    var file = clientContext.Web.GetFileByServerRelativeUrl("/Shared Documents/test.txt");
                    clientContext.Load(file, item => item.Name);
                    var versions = file.Versions;
                    var oldVersions = clientContext.LoadQuery(versions.Where(v => v != null));
                    if (oldVersions != null)
                        foreach (Microsoft.SharePoint.Client.FileVersion _version in oldVersions)
                            clientContext.Load(_version, item => item.VersionLabel);
                            if (!Directory.Exists(txtSaveTo.Text))
                            using (var wc = new System.Net.WebClient())
                                wc.UseDefaultCredentials = true;
                                string filePath = Path.Combine(@"c:\test\", Path.GetFileNameWithoutExtension(file.Name) + _version.VersionLabel + ".xml");
                                wc.DownloadFile("http://sp/sites/test"+ _version.Url, filePath);


    Best Regards

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact

    Thursday, January 5, 2017 9:04 AM