Click here to Skip to main content
14,176,894 members
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 24 Sep 2007
Licenced CPOL

Download all the versions of a file from a SharePoint document library

, 24 Sep 2007
Rate this:
Please Sign up or sign in to vote.
This code snippet can be used to download all the versions (if exists) of a document from within folders and subfolders inside a given document library.


Some days back, I submitted a code snippet named File Shunter that can be used to upload multiple files in a batch to a SharePoint (2003/2007) document library with meta data update and switching versioning On/Off. This code snippet (some of its parts I found on the net some time back) named Anti Shunter can be used to download all versions of all documents from a document library (up to any number of folders, if they exist), and it can be configured to retain the uploaded Date Created and Date Modified.


Some days back, we had to upgrade from SharePoint 2003 to MOSS 2007, and we maintain a huge document library for an electronic document system (approx 200,000 files with many versions), and none of the In-Place or other methods helped to upgrade to SP 2007. We maintain a number of meta data columns which are mainly composed of file names. So, I had to write some piece of code which could download all the versions of all files into a folder structure, retaining there actual Date Created and Date Modified and composing the file name as required from meta data columns.

Using the code

The following code can be used in a number of ways to get a single required file, all files, or a few files filtered by some meta data column or the Title/Name column. I have used the SharePoint 2007 Object Model (based on Microsoft.SharePoint.dll) and used the SPFile and SPWeb classes to access a file and then its versions, and each version required meta data. Say, a file has five versions in a folder named A, then this code will create a folder "A" with four subfolders named "Revision-4" "Revision-3", "Revision 2", "Revision 1", each folder containing the file version, and the current file will be in folder "A" outside of the other revision subfolders. So now, this folder structure can be uploaded again into a new document library or used for any other purpose required. The following function can be used recursively in order to access all the folders/subfolders within a document library. The Document Library columns named Date Created and Date Modified can be accessed and stored into variables, and while saving the files, using the FileSystem class, a file's Date Created and Modified can be updated.

Private Function AccessFolder(ByVal folder As SPFolder) As Long

    Dim strPath As String = String.Empty

    Dim lFolderSize As Long = 0

    If m_strExportDir <> String.Empty Then
        strPath = folder.ServerRelativeUrl
    End If

    Dim iTotalVersions As Integer = 0
    Dim iI As Integer = 0
    Dim FinalFileName As String = ""
    Dim dt_SPFileCreated As DateTime
    Dim s_FileName As String = ""

    For Each file As SPFile In folder.Files
      iTotalVersions = file.Versions.Count
      Dim versions As SPFileVersionCollection = file.Versions  
       For iI = 0 To iTotalVersions - 1
             iTotalDocs = iTotalDocs + 1         
             Dim version As SPFileVersion = versions(iI)
            ' Dim hash As System.Collections.Hashtable = _
            ' Dim keys As System.Collections.ICollection = hash.Keys
             s_FileName = file.Name
             dt_SPFileCreated = file.Versions(iI).Created 
             m_iTotalFileVersions += 1
            ' Now the above declared hash and keys can be used
            ' to acces each metadata columns
            ' as follows
            ' Dim key As Object
            'For Each key In keys
            'If key.ToString.ToLower = "Ur Custom Column Name" Then
                 ' Do whatever required
            ' End if 
           FinalFileName =
           System.IO.Directory.CreateDirectory(Pathtofilesystemtosave + _
                     "\Revision-" + m_iTotalFileVersions.ToString)
           Dim sFileLoc As String = path + "\Revision-" + _
               m_iTotalFileVersions.ToString + "\\" + FinalFileName
           Dim binFile As Byte() = version.OpenBinary()
           If binFile.Length > 0 Then
           Dim fs As FileStream = New FileStream(sFileLoc, _
                     FileMode.OpenOrCreate, FileAccess.Write)
           fs.Write(binFile, 0, binFile.Length)
           End If

       ' Now for current file
       iI = iI + 1
       iTotalDocs = iTotalDocs + 1
       Dim hash As System.Collections.Hashtable = file.Properties
       Dim keys As System.Collections.ICollection = hash.Keys
       s_FileName = file.Name
       dt_SPFileCreated = file.TimeCreated

       ' In the same way as above for versions, Key ca be declared to access 
       ' hash table for meta data column collection to access(skipping)
       FinalFileName = file.Name
       System.IO.Directory.CreateDirectory(Pathtofilesystemtosave + _
                        "\Revision-" + iI.ToString)
       Dim sFileLoc As String = Path + "\Revision-" + _
                 iI.ToString + "\\" + FinalFileName
       Dim binFile As Byte() = file.OpenBinary()
       If binFile.Length >= 0 Then
         Dim fs As FileStream = New FileStream(sFileLoc, _
                   FileMode.OpenOrCreate, FileAccess.Write)
         fs.Write(binFile, 0, binFile.Length)
       end if
End Function


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Junaid Raza
Web Developer
Pakistan Pakistan
An IT graduate with 3 years experience in the area of software development, Database designing and administration, SharePoint/MOSS 2003/2007 development using its standard web services and Object Model and Network Programming. Currently working as Regional Database Manager with Nokia Siemens Networks (Telenor Project)Pakistan.

You may also be interested in...


Comments and Discussions

Question$doc.versions.count versus $doc.file.versions.count Pin
Member 1198379614-Sep-15 10:40
memberMember 1198379614-Sep-15 10:40 
QuestionSaving All Versions of Documents Pin
mkoziol217-Dec-13 3:51
membermkoziol217-Dec-13 3:51 
GeneralSharepoint Dll Pin
AACINC16-Feb-09 6:28
memberAACINC16-Feb-09 6:28 
GeneralRe: Sharepoint Dll Pin
Junaid Raza16-Feb-09 18:40
memberJunaid Raza16-Feb-09 18:40 
GeneralRe: Sharepoint Dll Pin
AACINC17-Feb-09 4:10
memberAACINC17-Feb-09 4:10 
GeneralRe: Sharepoint Dll Pin
felymich10-Aug-11 14:39
memberfelymich10-Aug-11 14:39 
GeneralWeb Service Pin
tocik4-Dec-07 2:46
membertocik4-Dec-07 2:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190526.1 | Last Updated 24 Sep 2007
Article Copyright 2007 by Junaid Raza
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid