I have code to create a folder and add items to the folder . I have used CSOM to do this particulat operation . Since this is async code , sometimes the folder is not created and add items is getting fired with error . How to handle something like this? Is there any code example to create folder in list and add items and overcome above scenario.

function CreateNewfolder(loginuserid, strTitle) {

    var Context;
    var oWebsite;
    var oList;
    var itemCreateInfo;
    var myListItemCreationInfo;
    Context = new SP.ClientContext.get_current();
    oWebsite = Context.get_web();
    List = oWebsite.get_lists().getByTitle("List");
    var itemurl = "/Lists/" + List + "/" + loginUSERID;

        if (checkfolderexist() == false) {
            itemCreateInfo = new SP.ListItemCreationInformation();
            this.oListItem = List.addItem(itemCreateInfo);
            this.oListItem.set_item('Title', loginUSERID);
            this.oListItem.set_item('Name', strTitle);

            Context.executeQueryAsync(Function.createDelegate(this, successHandlerFolder), Function.createDelegate(this, errorHandlerFolder));

            function successHandlerFolder( {
                console.log(" Create Folder Method success");
                CreateNewFiles(loginUSERID, strTitle);
            function errorHandlerFolder() {
                console.log(" Create Folder Method fail " + JSON.stringify(error));

        else {


function CreateNewFiles(loginUSERID, strTitle) {
    var listName = "MyFavorites";
    var clientContext;
    var oWebsite;
    var oList;
    var itemCreateInfo;
    var myListItemCreationInfo;
    var clientContext = new SP.ClientContext.get_current();
    var Website = clientContext.get_web();
    oList = Website.get_lists().getByTitle("List");
    var myListItemCreationInfo = new SP.ListItemCreationInformation();
    var folderUrl = "/Lists/List/" + loginUSERID;
    this.newLinkItem = oList.addItem(myListItemCreationInfo);
    this.newLinkItem.set_item('Title', url);
    this.newLinkItem.set_item('Name', strTitle);
    clientContext.executeQueryAsync(Function.createDelegate(this, successHandler), Function.createDelegate(this, errorHandler));

    function successHandler() {


    function errorHandler() {


function checkfolderexist() {

    var listTitle = 'List';
    var fileUrl = "/Lists/List/" + loginUSERID;

    var ctx = SP.ClientContext.get_current();
    var list = ctx.get_web().get_lists().getByTitle(listTitle);
    var qry = new SP.CamlQuery();
    qry.set_viewXml('<View Scope="RecursiveAll"><Query><Where><Eq><FieldRef Name="FileRef"/><Value Type="Url">' + fileUrl + '</Value></Eq></Where></Query></View>');
    var items = list.getItems(qry);
    ctx.executeQueryAsync(function () {
        fileFound = (items.get_count() > 0);
        return fileFound;

    function (sender, args) {

  • the error handler of checkfolderexist() should return false also. May I give you REST API solution using jQuery? – Atish Dipongkor - MVP Oct 5 '15 at 5:00

checkfolderexist also makes an async call and can't return the result immediately. However, your code is treating as if checkfolderexist can return result synchronously.

You can logically structure your code like this:

  • checkfolderexist
    • executeQueryAsync success
      • items.get_count() > 0: Folder is found, just call CreateNewFiles
      • else: Create folder (your existing code)
        • executeQueryAsync of create folder success: call CreateNewFiles
        • executeQueryAsync of create folder fail: Show error
    • executeQueryAsync of checkfolderexist fail: Show error

You should probably rename your functions if you follow this structure.

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.