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'm trying to get file versions from an an SPListItem...

I tried in many way, looping on SPListItem.Versions or SPListItem.File.Versions, tring to return my byte stream..

I can retrieve file info's, like version, size etc, but whenever I try to get the "real" file of that version, i retrieve alway the last published...

This is a code draft:

List<byte[]> Files = new List<byte[]>();
var versions = item.File.Versions;

foreach (SPListItemVersion version in item.Versions)
{
     string author = version.CreatedBy.User.LoginName;
     DateTime lastMod= version.Created;
     long version = version.VersionId;

     foreach (SPFileVersion fileVersion in versions)
     {
            Files.Add(fileVersion.OpenBinary());
     }
 }

I tryed also accessing the SPListItem.Versions (not File.Versions) and accessing the file in many other ways... How could I access the file without "restore it"?

Thank you very much!

share|improve this question
add comment

3 Answers

There are (at least) two ways to get the content of an old version of a file, both go through the SPFileVersion from item.File.Versions.

If you want to use the content in your code, then you can call SPFileVersion.OpenBinary (or OpenBinaryStream)

If you want to allow the user to download the content then you can provide a link based on SPFileVersion.Url (needs to be prefixed with SPWeb.Url (or ServerRelativeUrl)).

Here is a small console app for listing the content of old versions of a text document as well as the urls to get them from the browser: var url = "http://sp2010/test/Shared%20Documents/test.txt";

using (var site = new SPSite(url))
{
    using (var web = site.OpenWeb())
    {
        var file = web.GetFile(url);
        Console.WriteLine("File Versions:");
        foreach (SPFileVersion version in file.Versions)
        {
            Console.WriteLine("version.VersionLabel: {0}", version.VersionLabel);
            var content = Encoding.UTF8.GetString(version.OpenBinary());
            Console.WriteLine("Content from version.OpenBinary : {0}", content);
            Console.WriteLine("version.url: {0}", version.Url);
            var fullVersionUrl = SPUrlUtility.CombineUrl(web.Url, version.Url);
            Console.WriteLine("fullVersionUrl: {0}", fullVersionUrl);
            Console.WriteLine();
        }
    }
}

In your code you loop the File.Versions inside the Item.Version, you shouldn't do that they are two parallel collections, similar to the SPFile and SPItem being parallel versions of the latest version.

share|improve this answer
    
Based on the documentation, it says the OpenBinary() makes that version the most recent version. Is the documentation just plain old wrong? msdn.microsoft.com/en-us/library/… –  Ants Apr 2 '13 at 3:42
add comment

could you try this piece of code?The below code retrieves the content of the wikipage(published)

     foreach (SPListItemVersion objVersion in objVerisionColl)
                    {

                      if (objVersion.ListItem.Versions.GetVersionFromLabel(objVersion.VersionLabel).IsCurrentVersion)
                        {
                            bool isPublished = objVersion.Level == SPFileLevel.Published ? true : false;
                            if (isPublished)
                            {

                                SPListItem itempubl = objVersion.ListItem;
                                prevWikiContent = itempubl.Versions.GetVersionFromLabel(objVersion.VersionLabel)["PublishingPageContent"].ToString();
                                return prevWikiContent;
                            }
                        }
share|improve this answer
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.