Update 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 update the file's contents you can use a FileCreationInformation object and set the overwrite attribute to true by using the set_overwrite() method, as shown in this example.

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


Figure 1: Open SP Site

After adding the JavaScript file, this file is available in the SharePoint Site Assets Folder.


Figure 2: Site Assets

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


Figure 3: Add a new Page


Figure 4: New Wiki 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: Content link

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


Figure 8: Save the Web Part


Figure 9: Update Succesfully

List updated successfully.

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