Using GetListItems to copy files

Aug 4, 2014 at 7:36 PM
Edited Aug 4, 2014 at 7: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 = "http://webapp.com/site/";
 var listName = "Shared Documents";
 console.log("Attempting to parse folder " + folderPath);

 ctx = new SP.ClientContext(siteURL);

 $().SPServices({
    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 12:45 PM
Take a look at the "Copy" operations (https://spservices.codeplex.com/wikipage?title=Copy). One of them should provide what you need. You can call the copy operation witin the "completefunc:" part of your GetListItems op.

Geoff
Coordinator
Aug 10, 2014 at 3: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:
http://sympmarc.com/2014/05/27/uploading-attachments-to-sharepoint-lists-using-spservices/

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

M.
Aug 12, 2014 at 2: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...
Coordinator
Aug 12, 2014 at 3:14 PM
SPServices - SharePoint's BAND-AID®.

M.