I have a requirement to copy the documents from one library to another while not loosing the version history.

I am aware thhat using the explorer view, I can drag and drop the files which retains the versions as well. But I want to retain the documents at the source as well.

I was looking at the SPFileVersionCollection. Using this, do I need to iterate over each version and then copy or there is any simpler way of doing this.


Yes you need to iterate through SPFileVersionCollection. Following is the sample code:

SPFile fileSource = itmSource.File;
/*Here we'll get the created by and created on values from the source document.*/
SPUser userCreatedBy = fileSource.Author;
/*Note we need to convert the "TimeCreated" property to local time as it's stored in the database as GMT.*/
DateTime dateCreatedOn = fileSource.TimeCreated.ToLocalTime();
//Get the versions
int countVersions = itmSource.File.Versions.Count;
/*This is a zero based array and so normally you'd use the < not <= but we need to get the current version too which is not in the SPFileVersionCollection so we're going to count one higher to accomplish that.*/
for (int i = 0; i <= countVersions; i++)
     Hashtable hashSourceProp;
     Stream streamFile;
     SPUser userModifiedBy;
     DateTime dateModifiedOn;
     string strVerComment = "";
     bool bolMajorVer = false;
     if (i < countVersions)
/*This section captures all the versions of the document and gathers the properties we need to add to the SPFileCollection.  Note we're getting the modified information and the comments seperately as well as checking if the version is a major version (more on that later).  I'm also getting a stream object to the file which is more efficient than getting a byte array for large files but you could obviously do that as well.  Again note I'm converting the created time to local time.*/
          SPFileVersion fileSourceVer = itmSource.File.Versions[i];
          hashSourceProp = fileSourceVer.Properties;
          userModifiedBy = (i == 0) ? userCreatedBy: fileSourceVer.CreatedBy;
          dateModifiedOn = fileSourceVer.Created.ToLocalTime();
          strVerComment = fileSourceVer.CheckInComment;
          bolMajorVer = fileSourceVer.VersionLabel.EndsWith("0") ? true : false;
          streamFile = fileSourceVer.OpenBinaryStream();
/*Here I'm getting the information for the current version.  Unlike in SPFileVersion when I get the modified date from SPFile it's already in local time.*/
          userModifiedBy = fileSource.ModifiedBy;
          dateModifiedOn = fileSource.TimeLastModified;
          hashSourceProp = fileSource.Properties;
          strVerComment = fileSource.CheckInComment;
          bolMajorVer = fileSource.MinorVersion == 0 ? true : false;
          streamFile = fileSource.OpenBinaryStream();
     string urlDestFile = libDest.RootFolder.Url + "/" + fileSource.Name;
/*Here I'm using the overloaded Add method to add the file to the SPFileCollection.  Even though this overload takes the created and modified dates for some reason they aren't visible in the SharePoint UI version history which shows the date/time the file was added instead, however if this were a Microsoft Word document and I opened it in Word 2010 and looked at the version history it would all be reflective of the values passed to this Add method.  I'm voting for defect but there could just be something I'm missing.*/
     SPFile fileDest = libDest.RootFolder.Files.Add(
     if (bolMajorVer)
/*Here we're checking if this is a major version and calling the publish method, passing in the check-in comments.  Oddly when the publish method is called the passed created and modified dates are displayed in the SharePoint UI properly without further adjustment.*/
/*Setting the created and modified dates in the SPListItem which corrects the display in the SharePoint UI version history for the draft versions.*/
          SPListItem itmNewVersion = fileDest.Item;
          itmNewVersion["Created"] = dateCreatedOn;
          itmNewVersion["Modified"] = dateModifiedOn;

Source: http://sharepointvenividivici.typepad.com/sharepoint-customization/2011/06/maintain-file-version-history-when-movingcopying-files-between-sharepoint-sites.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.