Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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)
{
if(!isCurrent) 
{
List LibraryName = clientContext.Web.Lists.GetByTitle(lib); 
clientContext.Load(LibraryName);
clientContext.ExecuteQuery();
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));
//clientContext.Load(collListItem);
clientContext.ExecuteQuery();

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
//http://social.technet.microsoft.com/Forums/de-DE/sharepointdevelopmentprevious/thread/88a05256-8694-4e40-863d-6c77512e079b
clientContext.ExecuteQuery();
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
add comment

2 Answers

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)

http://yoursite/yoursubsite/Documents/Book1.xlsx

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“

http://yoursite/yoursubsite/_vti_history/1/Documents/Book1.xlsx

“2” = version “0.2“

http://yoursite/yoursubsite/_vti_history/2/Documents/Book1.xlsx

“512” = version “1.0“

http://yoursite/yoursubsite/_vti_history/512/Documents/Book1.xls

“1025” = version “2.1“

http://yoursite/yoursubsite/_vti_history/1025/Documents/Book1.xls

share|improve this answer
add comment

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
add comment

Your Answer

 
discard

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.