Move/Migrate SharePoint list items/documents with metadata and version history

In this blog we discuss how to move/migrate document/files from one location to another whilst preserving its metadata properties and version history.

Follow the below methods to move files while maintaining all metadata properties and version history:

Method 1: Using Content and Structure to move files with versions and Metadata
  1. First select the documents you want to move/migrate.
  2. Then Go to Site Actions -> Site settings -> click on “Content and structure” under the Site Administration.
  3. After that open the SharePoint data library and then select the items that you want to move.
  4. Then you see the Move dialog box to specify the destination, you need to select the destination document library and then click OK.
  5. After this, you can see the documents in the destination document library.
Method 2: Programmatically – move document with version history

Using PowerShell to maintain file version history when moving/copying files between SharePoint sites

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();
     }
     else
     {
/*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(
                 urlDestFile, 
                 streamFile, 
                 hashSourceProp, 
                 userCreatedBy, 
                 userModifiedBy, 
                 dateCreatedOn, 
                 dateModifiedOn, 
                 strVerComment, 
                 true);
     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.*/
          fileDest.Publish(strVerComment);
     else
     {
/*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;
          itmNewVersion.UpdateOverwriteVersion();
     }
}

Source: http://julieturner.net/2011/06/maintain-file-version-history-when-movingcopying-files-between-sharepoint-sites/

Using the above methods you can preserve version history and metadata properties while migrating/moving files between document libraries.