Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Can anyone point me in the direction of downloading all the versions of a file in a SharePoint 2007 document library?

Is this possible with PowerShell, C#, or another way?

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

You can download the different file versions by keeping the folder structure as well:

private long AccessFolder(SPFolder folder)
{

    string strPath = string.Empty;

    long lFolderSize = 0;

    if (m_strExportDir != string.Empty) {
        strPath = folder.ServerRelativeUrl;
    }

    int iTotalVersions = 0;
    int iI = 0;
    string FinalFileName = "";
    DateTime dt_SPFileCreated = default(DateTime);
    string s_FileName = "";

    foreach (SPFile file in folder.Files) {
        iTotalVersions = file.Versions.Count;
        SPFileVersionCollection versions = file.Versions;
        for (iI = 0; iI <= iTotalVersions - 1; iI++) {
            iTotalDocs = iTotalDocs + 1;
            SPFileVersion version = 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;              
            FinalFileName = file.name;
            System.IO.Directory.CreateDirectory(Pathtofilesystemtosave + "\\Revision-" + m_iTotalFileVersions.ToString);
            string sFileLoc = path + "\\Revision-" + m_iTotalFileVersions.ToString + "\\\\" + FinalFileName;
            byte[] binFile = version.OpenBinary();
            if (binFile.Length > 0) {
                FileStream fs = new FileStream(sFileLoc, FileMode.OpenOrCreate, FileAccess.Write);
                fs.Write(binFile, 0, binFile.Length);
                fs.Close();
            }
        }

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

        FinalFileName = file.Name;
        System.IO.Directory.CreateDirectory(Pathtofilesystemtosave + "\\Revision-" + iI.ToString());
        string sFileLoc = Path + "\\Revision-" + iI.ToString() + "\\\\" + FinalFileName;
        byte[] binFile = file.OpenBinary();
        if (binFile.Length >= 0) {
            FileStream fs = new FileStream(sFileLoc, FileMode.OpenOrCreate, FileAccess.Write);
            fs.Write(binFile, 0, binFile.Length);
            fs.Close();
        }
    }
}
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.