0

I am trying to add a folder to a library on my site, however I am finding it extremely difficult to complete such a simple task. The function executes properly (no errors or exceptions) however the folder is never added and the executeQueryAsync function calls the error handler, making me think that the server denies the request.

        function createFolder() {
        var clientContext = SP.ClientContext.get_current();
        var oWebsite = clientContext.get_web();
        var oList = oWebsite.get_lists().getByTitle("Scripts Test");

        var itemCreateInfo = new SP.ListItemCreationInformation();
        itemCreateInfo.set_underlyingObjectType(SP.FileSystemObjectType.folder);
        itemCreateInfo.set_leafName("My new folder!");

        this.oListItem = oList.addItem(itemCreateInfo);
        this.oListItem.update();

        clientContext.load(this.oListItem);
        clientContext.executeQueryAsync(
            Function.createDelegate(this, successHandler),
            Function.createDelegate(this, errorHandler)
        );

    }

    function successHandler() {
        $("#requestStatus").html("Go to the " +
            "<a href='../Lists/Shared Documents'>document library</a> " +
            "to see your new folder.");
    }

    function errorHandler() {
        $("#requestStatus").html("Request Failed");
    }



    $("#addFolder").click(function () {
        createFolder();
        $("#requestStatus").html("maybe the list got updated");
        return false;
    });

Most of the code is taken straight from MSDN and implemented in a Visual Studio SP add in so I hesitate to think it's an issue with the code (although I would be happy if that was the problem). Could it be a problem with the server and my permissions to interact with it? Hopefully someone with SharePoint development experience can provide insight into the error.

1

You need set EnableFolderCreation to True before create folder in list. Add this code.

 var clientContext = SP.ClientContext.get_current();
 var oWebsite = clientContext.get_web();
 var list = oWebsite.get_lists().getByTitle("Scripts Test");

 //Enable Folder creation for the list
 list.EnableFolderCreation = true;
 list.Update();
 context.ExecuteQuery();

 //To create the folder in list
 ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();

 itemCreateInfo.UnderlyingObjectType = FileSystemObjectType.Folder;
 itemCreateInfo.LeafName = folderName;

 ListItem newItem = list.AddItem(itemCreateInfo);
 newItem["Title"] = folderName;
 newItem.Update();
 clientContext.ExecuteQuery();
  • unfortunately that did not fix it, it is still coming back as a failed request – cmoody4 Jan 30 '16 at 6:54
  • Working at my end. – Kaushal Khamar Jan 30 '16 at 8:09
0

Try out with below method. I had achieved with this code long back.

function AddFolderToSPList(folderName) {  
     var oWebsite;  
     var oList, oListItem;  
     var itemCreateInfo;  
     var clientContext = SP.ClientContext.get_current();
    var oWebsite = clientContext.get_web(); 
     oList = oWebsite.get_lists().getByTitle('ListName');  
     itemCreateInfo = new SP.ListItemCreationInformation();  
     itemCreateInfo.set_underlyingObjectType(SP.FileSystemObjectType.folder);  
     itemCreateInfo.set_folderUrl(targetSiteUrl + "/Lists/" + "ListName");  
     //if you want to add subfolder then add folder url like below commented line  
     //itemCreateInfo.set_folderUrl(targetSiteUrl + "/Lists/" + "ListName"+"/parentfolder");  
     oListItem = oList.addItem(itemCreateInfo);  
     oListItem.set_item('Title', folderName);  
     oListItem.update();  
     clientContext.load(oListItem);  
     clientContext.executeQueryAsync(  
       Function.createDelegate(this, successHandler()),  
       Function.createDelegate(this, errorHandler)  
     );  
   }  
   function successHandler() {  
     alert('success');  
   }  
   function errorHandler() {  
     alert("Request failed: message = " + arguments[1].get_message());  
   }  

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.