SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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); 
share|improve this question
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. – SPArchaeologist 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

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“


share|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

share|improve this answer
Could you provide some more details, please? – Stuart 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 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);
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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