3

In SharePoint 2013 I'm trying to add a script to move a folder from it's current location to a different parent folder while keeping the rest of the file structure the same. I am adding a button to the ECB to do this. Here is my relevant code:

var folder;

function runCode() {
    var clientContext = new SP.ClientContext.get_current();
    if (clientContext != undefined && clientContext != null) {
        this.web = clientContext.get_web();
        this.folder = web.getFolderByServerRelativeUrl(decodeURIComponent(ctx.rootFolder));
        clientContext.load(this.folder);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }
}

function onQuerySucceeded(sender, args) {
    var path = decodeURIComponent(ctx.rootFolder).replace('Open','Closed');
    this.folder.MoveTo(path, true);
    this.folder.update();
    window.location = path;
}

Is there a way to get the same functionality as SPFolder.MoveTo() in JavaScript? Or is there a better way of doing this?

3

JSOM SP.Folder Class does not provide MoveTo method as SSOM SPFolder class does.
But SP.File.moveTo(newUrl, flags) method could be utilized to move folder content (files) from source folder into destination as demonstrated below.

How to move files via JSOM

function moveFiles(folderSrcUrl,folderDescUrl,success,error)
{  
    var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    var folderSrc = web.getFolderByServerRelativeUrl(folderSrcUrl);
    context.load(folderSrc,'Files');
    context.executeQueryAsync(
        function() {
            //move files from source into destination folder 
            var files = folderSrc.get_files();
            var e = files.getEnumerator();
            while (e.moveNext()) {
              var file = e.get_current();
              var fileDescUrl = folderDescUrl + file.get_name(); 
              file.moveTo(fileDescUrl, SP.MoveOperations.overwrite);
            }
            folderSrc.deleteObject();  //delete source folder
            context.executeQueryAsync(
              function() {
                success();
              }, 
              error);
    }, 
    error);
}

Usage

//Example. Move files to archive
var srcFolderUrl = '/Shared Documents/Orders/';
var destFolderUrl = '/Shared Documents/Archive/';
moveFiles(srcFolderUrl,destFolderUrl,
   function(){
      console.log('Files are sent to archive');
   },
   function(sender,args){
      console.log(args.get_message());    
   }
);
3
  • Thanks for the reply! But, is there a way to move the all sub directories and files if they are unknown?
    – palepail
    Jul 2 '14 at 18:34
  • Unfortunately you cant move folders, but you could read source folder structure, then create target folders and move files to them. Jul 2 '14 at 18:43
  • Can I move files to SkyDrive using this code running in current page or in a SharePoint hosted app with tenant permissions? Jul 4 '14 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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