2015-08-26

SharePoint 2016 has the Copy / Move files or folders api using CSOM and JSOM.

The SP.js contains the following api’s,

  • SP.MoveCopyUtil.copyFile
  • SP.MoveCopyUtil.copyFolder
  • SP.MoveCopyUtil.moveFile
  • SP.MoveCopyUtil.moveFolder

Each of the method takes 3 parameters,

  • context – current context
  • source url – Full source url of the file / folder.
  • destination url – Full destination url of the file / folder.

How to copy a file in SharePoint using Javascript

SP.MoveCopyUtil.copyFile: This copies the specified file to the destination url.

var context = SP.ClientContext.get_current();
SP.MoveCopyUtil.copyFile(context,"http://sp2016/sitepages/home.aspx","http://sp2016/sitepages/home1.aspx");
context.executeQueryAsync(function(){},function(){});

The above code copies the /sitepages/home.aspx file to sitepages/home1.aspx with all the metadata including createdtime, author etc.

How to copy a folder in SharePoint using Javascript

SP.MoveCopyUtil.copyFolder: Copies the specified folder to the destination url including the files within 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 in SharePoint using Javascript

SP.MoveCopyUtil.moveFolder: Moves the specified folder to the destination.

	
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 file in SharePoint using Javascript

SP.MoveCopyUtil.moveFile: Moves the specified file to the destination url.

	
var context = SP.ClientContext.get_current();
SP.MoveCopyUtil.moveFile(context,"http://sp2016/sitepages/home1.aspx","http://sp2016/pages/home1.aspx");
context.executeQueryAsync(function(){},function(){});

The equivalent CSOM api is also available within the Microsoft.SharePoint.Client.MoveCopyUtil. The above api’s are also available within O365.

About the author 

Balamurugan Kailasam