Stop Custom Scripts: 4 Ways to Move SharePoint Files
At a Glance
- Target Audience
- SharePoint Developers
- Problem Solved
- Losing metadata & version history when copying/moving files via custom scripts
- Use Case
- Library migrations or reorganizing documents/folders across sites
Most developers waste hours writing custom scripts to move documents across SharePoint libraries. It never works perfectly. You write a loop, copy the items over, and suddenly realize you just destroyed the original metadata. Every file looks like it was created by the service account five minutes ago.
Stop overcomplicating your migrations. You do not need a massive custom script. You just need to use the native utility class.
SharePoint 2016 quietly introduced a solution that solves this exact headache. You can leverage the SP.MoveCopyUtil class in JSOM to handle the heavy lifting. It preserves your essential metadata, and it requires barely any code.
The SP.js library gives you four specific methods. Each one requires exactly three parameters to execute: your current context, the full source URL, and the full destination URL.
Here is exactly how to use them.
How to Copy a File Using JavaScript
You want to duplicate a file without losing its history. The SP.MoveCopyUtil.copyFile method pushes the file to your destination URL while keeping the original author and creation time intact.
var context = SP.ClientContext.get_current();
SP.MoveCopyUtil.copyFile(context, "http://sp2016/sitepages/home.aspx", "http://sp2016/sitepages/home1.aspx");
context.executeQueryAsync(function(){}, function(){});
How to Copy an Entire Folder
Moving individual files is fine. Copying an entire directory structure is where things usually break. Use SP.MoveCopyUtil.copyFolder to duplicate the folder and every single file nested inside it.
var context = SP.ClientContext.get_current();
SP.MoveCopyUtil.copyFolder(context, "http://sp2016/sitepages/homeFolder", "http://sp2016/sitepages/HomeFolder1");
context.executeQueryAsync(function(){}, function(){});
How to Move a Folder
Sometimes you do not want a copy. You need to physically relocate the asset. The SP.MoveCopyUtil.moveFolder method picks up the directory and drops it in the new location.
var context = SP.ClientContext.get_current();
SP.MoveCopyUtil.moveFolder(context, "http://sp2016/sitepages/homeFolder", "http://sp2016/pages/HomeFolder1");
context.executeQueryAsync(function(){}, function(){});
How to Move a Single File
For a simple file relocation, the SP.MoveCopyUtil.moveFile method handles the transfer instantly.
var context = SP.ClientContext.get_current();
SP.MoveCopyUtil.moveFile(context, "http://sp2016/sitepages/home1.aspx", "http://sp2016/pages/home1.aspx");
context.executeQueryAsync(function(){}, function(){});
You do not have to write this in JavaScript if you prefer C#. The equivalent CSOM utility exists under Microsoft.SharePoint.Client.MoveCopyUtil. It shares the same underlying logic but uses a slightly different parameter count.
And if you are working in the cloud, these exact APIs are fully supported in SharePoint Online.
Stop fighting the platform. Use the native tools. Save your time for the actual hard work.

