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.

(Visited 1,248 times, 1 visits today)

About the author 

Balamurugan Kailasam

Leave a Reply

  1. Hello,

    I used C # the CopyFile method. After copying, the source item becomes inaccessible with GetItemByd method. On error “File Not Found”. An idea of the cause ???

    Thank you.

  2. Hi,

    i m trying to do copy folder with REST api like this

    /_api/SP.MoveCopyUtil.CopyFolder

    POST data is 

    {“srcUrl”:”https://test.sharepoint.com/_api/Shared Documents/CONS0006/PermWPS”,”destUrl”:”https://test.sharepoint.com/_api/Shared Documents/CONS0006/6″}

    In response it is showing 

    CopyFolder=(null)

    And no folder copied 

    Any idea?

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

2 Free Ebooks: How to govern Microsoft teams 

Download your 2 free Microsoft Teams governance Ebooks and learn the steps necessary to create a bullet-proof governance strategy.