Create a File in SharePoint 2013 Using JavaScript Object Model (JSOM)

You can use the SharePoint client object model to retrieve, update and manage data in SharePoint 2013. SharePoint makes the object model available in several forms.

  • .NET Framework redistributable assemblies
  • JavaScript library
  • REST/OData endpoints
  • Windows Phone assemblies
  • Silverlight redistributable assemblies

This article shows how to do basic operations using the JavaScript object model. You can add a reference to the object model using HTML <script> tags.

The following sections describe tasks that you can complete programmatically and they include JavaScript code examples that show the operations.

To create files, you use a FileCreationInformation object, set the URL attribute and append content as a base64 encoded array of bytes, as shown in this example.

Procedure

Open your SP Site in SharePoint 2013 Designer, then select Site Assets icon in the designer ribbon and add a JavaScript file.


Figure 1: Add a JavaScript File

After adding a JavaScript file, this file will be available in the SharePoint Site Assets Folder.


Figure 2: Site Assets Folder

Go to the SP site Page and add a new page to the SharePoint site.


Figure 3: SP Site


Figure 4: Add a New Page

After adding a page select an Insert button in the Ribbon Menu.


Figure 5: Ribbon Menu

Then add a Content Editor Web part into the page.


Figure 6: Content Editor

Edit the WebPart and add a JavaScript file link to the content link .


Figure 7: Web Part

Save the web part and save the page in the site. Click the button!


Figure 8: Save the web Part


Figure 9: list updated succesfully

List updated successfully.

Source Code

  1. < div > < button onclick = " createfile ()" > Click here to createfile < /button></div >  
  2. < div id = " resultpanel " > < /div>  
  3. <script type="text/javascript  
  4. ">   
  5. function createfile (resultpanel)   
  6. {  
  7. var clientContext;  
  8. var oWebsite;  
  9. var oList;  
  10. var fileCreateInfo;  
  11. var fileContent;  
  12. clientContext = new SP.ClientContext.get_current();  
  13. oWebsite = clientContext.get_web();  
  14. oList = oWebsite.get_lists().getByTitle("  
  15. Shared Documents ");  
  16. fileCreateInfo = new SP.FileCreationInformation();  
  17. fileCreateInfo.set_url("  
  18. my new file.txt ");  
  19. fileCreateInfo.set_content(new SP.Base64EncodedByteArray());  
  20. fileContent = "  
  21. The content of my new file ";  
  22. for (var i = 0; i < fileContent.length; i++)   
  23. {  
  24. fileCreateInfo.get_content().append(fileContent.charCodeAt(i));  
  25. }  
  26. this.newFile = oList.get_rootFolder().get_files().add(fileCreateInfo);  
  27. clientContext.load(this.newFile);  
  28. clientContext.executeQueryAsync(  
  29. Function.createDelegate(this, successHandler),  
  30. Function.createDelegate(this, errorHandler)  
  31. );  
  32. function successHandler()   
  33. {  
  34. resultpanel.innerHTML =  
  35. "  
  36. Go to the " +  
  37. " < a href = '../Lists/Shared Documents' > document library < /a> " +  
  38. "to see your new file.";  
  39. }  
  40.   
  41. function errorHandler()   
  42. {  
  43. resultpanel.innerHTML = "Request failed: " + arguments[1].get_message();  
  44. }  
  45. }  
  46. </script >  
Thanks for reading my article.