|
|||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionSome 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. BackgroundSome 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 codeThe 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 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 = _
file.Versions(iI).Properties
' 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 = 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
' 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)
fs.Close()
end if
Next
End Function
|
||||||||||||||||||||||||||||||||||||||||