Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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());    
   }
);
share|improve this answer
    
Thanks for the reply! But, is there a way to move the all sub directories and files if they are unknown? –  palepail Jul 2 at 18:34
    
Unfortunately you cant move folders, but you could read source folder structure, then create target folders and move files to them. –  Vadim Gremyachev Jul 2 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? –  Sekhar2815 Jul 4 at 17:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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