0

This is wierd, i have created a folder in site asset programmatically by JSOM. Then i created a function to see if my folder exist or not (In the GUI its visible).

My function returns null but if i create the folder in the GUI by myself it returns true, it recoginize it. What can be wrong here, im on sharepoint 2013 online and all my code is executing from a sharepoint-hosted app..

  var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
  var currentContext = new SP.ClientContext.get_current();
  var hostContext = new SP.AppContextSite(currentContext, hostUrl);
  var hostweb = hostContext.get_web();

  function checkIfContentExist(assetLibrary, folderTitle, listTitle, complete, error) {
            var listExist = false;
            var folderExist = false;
            var listCollection = hostweb.get_lists();
            var list = hostweb.get_lists().getByTitle(assetLibrary);
            var query = new SP.CamlQuery.createAllItemsQuery();
            var collListItem = list.getItems(query);
            currentContext.load(collListItem);
            currentContext.load(listCollection);
            currentContext.executeQueryAsync(function () {
                var itemEnumerator = collListItem.getEnumerator();
                while (itemEnumerator.moveNext()) {
                    var currentItem = itemEnumerator.get_current();
                    console.log(currentItem);
                    console.log(currentItem.get_item("Title"));
                    if (currentItem.get_item("Title") === folderTitle) {
                        folderExist = true;
                       // break;
                    }
                }
                var listEnumerator = listCollection.getEnumerator();
                while (listEnumerator.moveNext()) {
                    var currentList = listEnumerator.get_current();
                    if (currentList.get_title() === listTitle) {
                        listExist = true;
                      //  break;
                    }
                }
                if (folderExist || listExist) {
                    complete(true);
                } else {
                    complete(false);
                }
            },
            function (args, sender) {
                error(args);
            });
        }

   function provisionFolders() {

    var folderName = "newswebpart";
    var folderNameScripts = "Scripts";
    var folderNameLibrary = "Images";

    var list = hostweb.get_lists().getByTitle('Site Assets');
    var itemCreationInformation = new SP.ListItemCreationInformation();
    itemCreationInformation.set_underlyingObjectType(SP.FileSystemObjectType.folder);
    itemCreationInformation.set_leafName(folderName);

    var folderItem = list.addItem(itemCreationInformation);
    folderItem.update();
    list.update();
    currentContext.load(folderItem);
    currentContext.executeQueryAsync(onSuccess(folderNameScripts), onFailed);
  }
 function onSuccess(folderNameScripts) {
 console.log("Success: Folder created for files");
 }
 function onFailed(sender,args){
 console.log(args.get_message());
 }
  • Please post your code to create folder and check the folder exists – Unnie Oct 23 '14 at 8:22
  • I have post med code now – Anders Oct 23 '14 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.