Welcome to MSDN Blogs Sign in | Join | Help

SharePoint: How to retrieve specific version of file from a document library thro’ programmatically?

The SPFile.Versions API can be used to retrieve the specific version of the document library.

 

I have compiled a sample code snippet to retrieve the specific version of the document library.

 

            //Open the web

            SPWeb web = new SPSite("http://karthickmain:9091/sites/siteb").OpenWeb();

            //Get the folder

            SPFolder folder = web.Folders["Shared Documents"];

            //Get the file

            SPFile file = folder.Files["abc.doc"];

            //Get all the versions

            SPFileVersionCollection versions = file.Versions;

            //Get the first version

            SPFileVersion version = versions[3];

           

            //Get the data

byte [] dBytes = version.OpenBinary();

 

 

How to restore a specific version back to the document library thro’ programmatically?

 

There are two ways we can restore the document.

1.      SPFileVersionCollection.Restore() method

                        //restore specific version

versions.Restore(3);

 

2.      SPFile.SaveBinary() method

file.SaveBinary(versions[3].OpenBinary());

 

But however the Restore method is more efficient as per msdn

 

LINKS:

=======

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/spptsdk/html/tscSPFileVersionCollection_SV01013561.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/spptsdk/html/tsmSPFileVersionCollectionRestore_SV01013756.asp

Published Tuesday, March 28, 2006 3:47 PM by Karthikeyan
Filed under:

Comments

# re: SharePoint: How to retrieve specific version of file from a document library thro’ programmatically?

Thursday, June 29, 2006 9:41 AM by BobC
Thanks Karthick.  

# re: SharePoint: How to retrieve specific version of file from a document library thro’ programmatically?

Thursday, November 27, 2008 4:09 AM by qzb

Great!But will the SPFileVersion Object exist in database if the SPFile object is deleted?

# programmatically get an old document version | keyongtech

Wednesday, January 21, 2009 10:10 PM by programmatically get an old document version | keyongtech
Anonymous comments are disabled
 
Page view tracker