In my project I am migrating Sharepoint items from source site to destination site. I need to maintain all versions of the source file. My approach for the migration of sharepoint item is as follow.

  1. Read source file data
  2. Create new file at destination
  3. write file data at destination file.
  4. Read source file version one by one
  5. Add version at destination file.

Here I am able to upload the file version. But my it overwrites the current version of destination file. I need to add the file version without changing its current version. How can I achieve it?

using (MemoryStream memoryStream = new MemoryStream(srcVersiondataBytes))
    SPFile.SaveBinaryDirect(clientcontext, destServerRelativeUrl, memoryStream, true);

You will need to use the prime API (content migration API) for this.

Basically you need to run an export and an import of the data you want to move . The gotcha is that you need to include the user security for this to work.

Here are some pointers for custom powershell: https://msdn.microsoft.com/en-us/library/office/bb249963(v=office.14).aspx

But if you are lazy you can just use the export web function.


Using below code to migrate SharePoint list items/documents with metadata and version history .

SPFile fileSource = itmSource.File;

SPUser userCreatedBy = fileSource.Author;

DateTime dateCreatedOn = fileSource.TimeCreated.ToLocalTime();

int countVersions = itmSource.File.Versions.Count;

for (int i = 0; i <= countVersions; i++)
   Hashtable hashSourceProp;
   Stream streamFile;
   SPUser userModifiedBy;
   DateTime dateModifiedOn;
   string strVerComment = "";
   bool bolMajorVer = false;
   if (i < countVersions)

      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();

      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;

   SPFile fileDest = libDest.RootFolder.Files.Add(
   if (bolMajorVer)


      SPListItem itmNewVersion = fileDest.Item;
      itmNewVersion["Created"] = dateCreatedOn;
      itmNewVersion["Modified"] = dateModifiedOn;

Simply you can use Sharepoint Migration API for this and there you need to create the Manifest.xml with the versions you need to migrate. I recommend that it will be faster than normal CSOM code.

You can refer to this article for more details on how to migrate using c# and migrations API http://case.schollaart.net/2016/03/10/office365-migration-using-c-sharp.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.