Using GetListItems to copy files

Aug 4, 2014 at 6:36 PM
Edited Aug 4, 2014 at 6:37 PM
I'm using the following code to get all of the items in a folder. Currently I'm creating an array of the file urls, then calling a function elsewhere that loops through that array and copies each file to a fixed destination. Since I'm already looping through the files here, though, is there any way to just copy each file as the service returns them?
function parseFolder(fldr) {
 var folderPath = fldr;
 var folderParts = folderPath.split('/');
 var folderName = folderParts[(folderParts.length)-1];
 var ctx, file, notifyId;
 var siteURL = "";
 var listName = "Shared Documents";
 console.log("Attempting to parse folder " + folderPath);

 ctx = new SP.ClientContext(siteURL);

    operation: "GetListItems",
    async: false,
    listName: listName,
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
    CAMLQueryOptions: "<QueryOptions><Folder>" + folderPath + "</Folder></QueryOptions>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
        var itemInfo = $(this).attr("ows_FileRef");
    var itemUrl = itemInfo.substring(itemInfo.indexOf(';#')+2)
        console.log("Found item: " + itemUrl);
    subarray = subarray + "/" + itemUrl + "$";
 console.log('Folder parsed.'); 
 return true;
Aug 7, 2014 at 11:45 AM
Take a look at the "Copy" operations ( One of them should provide what you need. You can call the copy operation witin the "completefunc:" part of your GetListItems op.

Aug 10, 2014 at 2:39 AM
GetListItems just passes back the URL of the file, not the file data itself. You need to open the file and copy it. See my blog post:

The Copy Web Services leaves a lot to be desired. It maintains a link between the two files that's impossible to break.

Aug 12, 2014 at 1:48 PM
Having the link between files wouldn't work for this project, so instead I used a REST call to copy the file. The final script is a bit of a bash-job, but I'm still using several SPServices calls for other operations in it. I can't seem to remember how we got along before SPServices...
Aug 12, 2014 at 2:14 PM
SPServices - SharePoint's BAND-AID®.