SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.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);
    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
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);
        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
              function() {


//Example. Move files to archive
var srcFolderUrl = '/Shared Documents/Orders/';
var destFolderUrl = '/Shared Documents/Archive/';
      console.log('Files are sent to archive');
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 '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. – Vadim Gremyachev 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? – sekhar Jul 4 '14 at 17:04

Your Answer


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.