0

Regarding the link below Create folders and subfolders in document library using client object model

can i get the same functionality achieved using JSOM/ECMA SCRIPT

3

Here is the example from original post converted to JSOM API:

    function createFolder(list,folderUrl){
        var createFolderInternal = function(parentFolder,folderUrl){
             var ctx = parentFolder.get_context();        
             var folderNames = folderUrl.split("/");
             var folderName = folderNames.shift();
             var folder = parentFolder.get_folders().add(folderName);
             ctx.load(folder);
             return executeQuery(ctx)
             .then(function(){
                 if (folderNames.length > 0){
                     return createFolderInternal(folder, folderNames.join("/"));
                 }         
                 return folder;     
             });
        };
        return createFolderInternal(list.get_rootFolder(), folderUrl);
    }

    function executeQuery(context) {
       return new Promise(function(resolve, reject){
            context.executeQueryAsync(function () {
                resolve();
            }, function (sender, args) {
                reject(args);
            });
      });
   }

The example demonstrates how to create the following folder hierarchy under Documents library:

Documents library 
   |
 Archive (folder)
   |   
  2018 (folder)
     |
     03 (folder)

Example

var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle("Documents");
createFolder(list,"Archive/2018/03")
.then(function(folder){
     console.log(folder.get_serverRelativeUrl());        
});

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.