I want to create file in a document set with JavaScript / jQuery.

function createfile (url)   
    var clientContext;  
    var oWebsite;  
    var oList;  
    var fileCreateInfo;  
    var fileContent;  

    clientContext = new SP.ClientContext.get_current();  
    oWebsite = clientContext.get_web();  
    oList = oWebsite.get_lists().getByTitle("Documents"); 

    fileCreateInfo = new SP.FileCreationInformation();  
    fileCreateInfo.set_url("my new file.txt ");  
    fileCreateInfo.set_content(new SP.Base64EncodedByteArray());  
    fileContent = url;  

    for (var i = 0; i < fileContent.length; i++)   

    this.newFile = oList.get_rootFolder().get_files().add(fileCreateInfo);  
    clientContext.executeQueryAsync( Function.createDelegate(this, successHandler), Function.createDelegate(this, errorHandler));  

    function successHandler()   
       console.log("FILE CREATED!");

    function errorHandler()   
       console.log("File Creation Failed: " + arguments[1].get_message());  

This will create the file in the "Documents" lists. My goal is to create the file directly in the document set which is called "MyTest".

With the SharePoint Client Browser I can see that the folder is located at: Documents / Root Folder / MyTest

1 Answer 1


I haven't tested this, but have you tried setting the filename to the location of the document set? So instead of:

fileCreateInfo.set_url("my new file.txt ");  

You would have:

fileCreateInfo.set_url("/Root Folder/MyTest/Myfile.text");  

I remember doing something similar in C# before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.