Create A New File Inside The Document Set In SharePoint 2013 Programatically Using JSOM

Document set

To create and manage multi-document work products consistently, we can use document set. For this, we need to configure a new document set content type for each multi-document work product, which they typically create.

The document set content type is a folder-based content type, which organizes multiple related documents into a single view, where they can be worked on and managed as a single entity.

Include JS file given below before we execute the function.

sp.documentmanagement.s

Site Collection administrator is there to activate the document sets feature before you create or configure new document set content types.

Go to the top-level site in the Site Collection for which you want to enable document sets.
  • Site Actions menu-->click Site Settings.
  • Site Collection Administration-->click Site collection features.
  • Find document sets in the list and then click Activate.
Call the reusable function 
  1. SP.SOD.executeFunc('sp.documentmanagement.js''SP.DocumentSet.DocumentSet'function() {  
  2.   
  3.    createfile(_spPageContextInfo.webAbsoluteUrl);  
  4.   
  5.    });  
  6. shareimprove this answer  
  7.   
  8. function createfile (url)     
  9. {    
  10.     var clientContext;    
  11.     var oWebsite;    
  12.     var oList;    
  13.     var fileCreateInfo;    
  14.     var fileContent;    
  15.   
  16.     clientContext = new SP.ClientContext.get_current();    
  17.     oWebsite = clientContext.get_web();    
  18.     oList = oWebsite.get_lists().getByTitle("TutorialDocuments");   
  19.   
  20.     fileCreateInfo = new SP.FileCreationInformation();    
  21.     fileCreateInfo.set_url("/Root Folder/GowthamDocuments/Sample file.docx");   
  22.     fileCreateInfo.set_content(new SP.Base64EncodedByteArray());    
  23.     fileContent = url;    
  24.   
  25.     for (var i = 0; i < fileContent.length; i++)     
  26.     {    
  27.         fileCreateInfo.get_content().append(fileContent.charCodeAt(i));    
  28.     }  
  29.   
  30.     this.newFile = oList.get_rootFolder().get_files().add(fileCreateInfo);    
  31.     clientContext.load(this.newFile);    
  32.     clientContext.executeQueryAsync( Function.createDelegate(this, successHandler), Function.createDelegate(this, errorHandler));    
  33.   
  34.     function successHandler()     
  35.     {    
  36.        console.log(" New File Added!");  
  37.     }    
  38.   
  39.     function errorHandler()     
  40.     {    
  41.        console.log("File Creation Failed: " + arguments[1].get_message());    
  42.     }    
  43. }   
Thanks for reading my blog. Please post your feedback.