Trying to find a way to copy folders(with documents inside) and files from a SharePoint Online Library to another, need to preserve the metadata of the files as well. The new modernized experience on SharePoint has a built in feature to do that, but I have a classic experience site that has that feature restricted, so need to find a way to do it there, ideally triggered from a button in the ribbon. Any help will be very much appreciated!

  • Anyone with an idea to help me solve this? Your help will be very much appreciated, thanks in advance! Sep 14, 2017 at 6:13
  • It requires file path instead of folder name. Use rest to iterate over the libray and copy each file into another
    – nbi
    Sep 15, 2017 at 14:44
  • Hi and thank you for your response, I intentionally put folder names instead of urls. I made the code work, but it is working only for a single file and what I'm trying to do is when multiple items are selected to copy them to the new location. I'm using the getSelectedItems method to get the current selected items, but can't figure out how to get the url of the files and pass it inside of the movecopyutil method. Any ideas? If you know how to do that, do please send me a code snippet that will work with my code, would be very much appreciated, thanks in advance! Sep 15, 2017 at 14:52

2 Answers 2


After update 2

I tested it in my SPO and its working correctly.


  1. List view webpart
  2. File name is present in the list view webpart

enter image description here

    function copyFiles() {
        var current_context = SP.ClientContext.get_current();
        var web = current_context.get_web();
        var selectedItems = SP.ListOperation.Selection.getSelectedItems();
        for (var i = 0; i < selectedItems.length; i++) {
            var id = selectedItems[i].id;
            var element = document.getElementById(id).children[0];
            var filePath = "";
            var fileName = "";
            if (element) {
                filePath = element.href;
                fileName = element.href.split('/').pop();
                var output = "https://mySiteUrl.sharepoint.com/DocumentLibrary/" + fileName;
                SP.MoveCopyUtil.copyFile(current_context, filePath, output, true);
                current_context.executeQueryAsync(function () { 
                    PageCopySuccess(); }, function () { PageCopyFailure();
                function PageCopySuccess() {
                function PageCopyFailure() {

<input type="button" value="Copy Files" style="cursor:pointer;" onclick="copyFiles()" />
  • Some how I am unable to properly format the code.. Strange !
    – nbi
    Sep 18, 2017 at 10:59
  • Hi nbi, just updated my post, put together the full code for the task, but it is not working and returning that the function copyFiles() is not defined. If you could please have a look and let me know where my mistake is. thank you in advance! I'm putting this code in a script editor web part in order to create a button in the library and trigger the code and the function from it. Kind Regards! Sep 18, 2017 at 12:26
  • Thank you nbi! I found a way to make it work for selected files, now trying to make it work for selected folders as well. And although the SP.MoveCopyUtil.copyFolder is similar to SP.MoveCopyUtil.copyFile, it is a bit different in terms of the parameters you pass inside where the source and destination parameters are supposed to be, as I'm trying to make it work to copy selected folders. Do you know a way to do that for selected folders, I tried to get the absolute url of the folder and pass it inside, but it didn't work? Sep 19, 2017 at 15:02

Found a solution for selected multiple files in a library to be copied to another library in the same sub-site, here is the tested and working code, it is for selected files only, the syntax for folders will be similar, the SP.MoveCopyUtil.copyFile will become SP.MoveCopyUtil.copyFolder:

<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<input type="button"  value="Copy Files" style="cursor:pointer;" onclick="copyFiles()" />
<script type="text/javascript">
    function copyFiles() 
        var context = SP.ClientContext.get_current();
        var selectedItemIds = SP.ListOperation.Selection.getSelectedItems(context); 

        var output = "Destination Library";

        for (idx in selectedItemIds)
            SP.MoveCopyUtil.copyFile(context, $("#" + selectedItemIds[idx].id + " a")[0].href.toString(), output + $("#" + selectedItemIds[idx].id + " a")[0].href.split("/")[$("#" + selectedItemIds[idx].id + " a")[0].href.split("/").length - 1].toString(), true );

        context.executeQueryAsync(function(){alert("OK")}, function(){alert("NOT OK")});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.