3

I'm using the function below to create a folder and I'm trying to make it also create a subfolder within the newly created folder.

CreateFoldersbyparent("abc","")
CreateFoldersbyparent("abc","\test")

function CreateFoldersbyparent(name,parent,callback){
    var documentLibraryName = "Delte dokumenter";  
    var fileCollectionEndpoint = "https://gbas.sharepoint.com/Test/_api/web/getfolderbyserverrelativeurl('" + documentLibraryName + "')/folders/add(url=\'" + name + "\\" + parent + "\')";
    $.ajax({
        url: fileCollectionEndpoint,
        type: "POST",
        async: false,
        headers: {
            "X-RequestDigest":  window.top.$("#__REQUESTDIGEST").val(),
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose"
        },
        success: function (data) {
            console.log("done")
        },
        error: function (jqXHR, textStatus, errorThrown) {
                        //handle error...
                        console.log(jqXHR);
        }
    });
}

Problem is that it created 2 folders in the main root folder of "Delte dokumenter"(Shared documents in English)

What I desire:

-Delte dokumenter
   -abc
      -test

What it does:

-Delte dokumenter
   -abc
   -abctest

What am I doing wrong?

0
var createFolder = function(listTitle, folderUrl,success,error)
{
var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle(listTitle);
var createFolderInternal = function(parentFolder,folderUrl,success,error)
{
   var ctx = parentFolder.get_context();
   var folderNames = folderUrl.split('/');
   var folderName = folderNames[0];
   var curFolder = parentFolder.get_folders().add(folderName);
   ctx.load(curFolder);
   ctx.executeQueryAsync(
     function () {
        if (folderNames.length > 1)
        {
           var subFolderUrl = folderNames.slice(1,folderNames.length).join('/');
           createFolderInternal(curFolder, subFolderUrl,success,error);
        }  
        success(curFolder);
     }, 
     error);
};
createFolderInternal(list.get_rootFolder(), folderUrl,success,error);
};
createFolder('DocumentLibraryName','Test/2017/12',
function(folder){
  console.log(String.format("Folder '{0}' has been created",folder.get_name()));
},
function(sender,args){
  console.log(args.get_message());
});

It will create folder structure as below:
Test
    |  
     2017
        |
         12
0

Did update based on your code logic.

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script type="text/javascript">
        function testCreateFolder() {
            CreateFoldersbyparent("abc", "MyDoc/").done(function () {
                CreateFoldersbyparent("test", "MyDoc/abc/");
            })
        }
        function CreateFoldersbyparent(name, parent, callback) {
            var deferred = $.Deferred();
            var documentLibraryName = "MyDoc";
            var fileCollectionEndpoint = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getfolderbyserverrelativeurl('" + documentLibraryName + "')/folders/add('" + parent+ name + "')";
            $.ajax({
                url: fileCollectionEndpoint,
                type: "POST",
                async: false,
                headers: {
                    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                    "accept": "application/json;odata=verbose",
                    "content-type": "application/json;odata=verbose"
                },
                success: function (data) {
                    console.log("done");
                    deferred.resolve(data);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    //handle error...
                    console.log(jqXHR);
                }
            });
            return deferred.promise();
        }
    </script>
    <input id="Button1" onclick="testCreateFolder()" type="button" value="button" />

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.