Introduction
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.
Background
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)
file.Versions(iI).Properties
s_FileName = file.Name
dt_SPFileCreated = file.Versions(iI).Created
m_iTotalFileVersions += 1
FinalFileName = file.name
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)
fs.Close()
End If
Next
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
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)
fs.Close()
end if
Next
End Function