I am trying to download the previous version of a document from _vti_history location of the file using client object model but the OpenBinaryDirect is giving

not found 402 error

for URL.

My code is as given below

public int GetStreamFromFile(string docid, string lib, string fileurl, ClientContext clientContext, int iuserid, string Version, bool isCurrrent)
List LibraryName = clientContext.Web.Lists.GetByTitle(lib); 
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "" + fileurl +
Microsoft.SharePoint.Client.ListItemCollection collListItem = LibraryName.GetItems(camlQuery);
clientContext.Load(collListItem, items => items.Include(item => item.Id, item => item["FileLeafRef"], item => item["LinkFilename"],
item => item["FileRef"], item => item["File_x0020_Size"], item => item["DocIcon"], item => item.File.Versions));

foreach (Microsoft.SharePoint.Client.ListItem oListItem in collListItem)
//string fileurl1 = (string)oListItem["FileRef"]; 
//string filename = (string)oListItem["LinkFilename"];

foreach (FileVersion version in oListItem.File.Versions)
if (Version == version.VersionLabel)
//Added excutequery to get object one more time as per blog
FileInformation fileInformation = ClientOM.File.OpenBinaryDirect(clientContext,version.Url);
bytesarr = ReadFully(fileInformation.Stream); 
  • Even I am facing the same issue. When I try to download a historical file using OpenBinaryDirect(), I get an error mentioning "The remote server returned an error: (404) Not Found.". However, If I pass the same URL through web browser, I am able to download the file. Couple of more points: 1. I even tried to modify the URL a bit by adding "ctx.Web.ServerRelativeUrl + "/" + version.Url" for the Relative URL 2. Tried to modify the URL by adding just the "/" – user17721 Jun 19 '13 at 18:27
  • did you get any solutions for this problem? I am seeing the same problem. – user19125 Aug 26 '13 at 5:43
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. – BlueSoul Aug 26 '13 at 12:46

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

...and use shushank sharma's solution to get the content.

While it's not as sexy as using the CSOM for the entire solution, it'll get you past this hurdle and on to the next! :-)

See below for an explanation of the version numbers ("512" and "1024" in the URLs above), taken from http://social.msdn.microsoft.com/Forums/sharepoint/en-US/99261c97-efda-4061-9bf9-10ec47923c36/where-are-version-history-documents-stored.

Canonical path (top-most version)


revision paths

You could access the previous version document like following.

http://yoursite/yoursubsite/_vti_history/512/Documents/Book1.xlsx (Accessing version 1.0)

One thing to note if you have only one version of the document then this will give 404 page not found error. You must use the canonical path to retrieve the top-most version.

_vti_history - Virtual directory

512 - See the following table then you could understa

“1” = version “0.1“


“2” = version “0.2“


“512” = version “1.0“


“1025” = version “2.1“


| improve this answer | |

This worked for me (I have 3 textboxes - Web Url / Item Url / Save To Path):

using (ClientContext clientContext = new ClientContext(txtWebUrl.Text))
                Web web = clientContext.Web;
                var file = clientContext.Web.GetFileByServerRelativeUrl(txtItemUrl.Text);

                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(txtSaveTo.Text, Path.GetFileNameWithoutExtension(file.Name) + _version.VersionLabel + ".xml");
                            wc.DownloadFile(txtWebUrl.Text + _version.Url, filePath);
| improve this answer | |

By far the best example with sample code:


| improve this answer | |

Use WebClient to download Different versions of file.

Edit: Sorry for Late Answer This is snipest of C# Code.

System.Net.WebClient client = new System.Net.WebClient();

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

Now you can parse data using Data object of System.IO.Stream class with its functions like(Data .ReadByte(); which read byte by byte). Find more reference for System.IO.Stream on MSDN

| improve this answer | |
  • Could you provide some more details, please? – Stu Pegg Sep 2 '13 at 7:32
  • This answer does not really help if you do not add more details (like a code snippet) – Robert Lindgren Sep 2 '13 at 8:28

This works for me with SharePoint 2013 with Claims - the user agent header was the key for me.

        private System.Net.WebClient SPWebClient() {

            if (_SPWebClient != null) return _SPWebClient;

            _SPWebClient = new WebClient {

                Credentials = new NetworkCredential(_sharePointUser, _sharePointPassword, _sharePointDomain)


            // for claims based auth
            _SPWebClient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");

            _SPWebClient.Headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MDDC)";

            return _SPWebClient;


        var versions = file.Versions;



        foreach (var version in versions) {

            var versionStream = this.SharePointWebClient.OpenRead($"{this.SharePointUrl}/{version.Url}");

            // do something useful

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.