This article explains how to update a file in a Document Library in SharePoint 2013 using CSOM-JavaScript.
Prerequisites
- Ensure you have access to the Office 365 online.
- Ensure the Napa Tool is available in your site.
Use the following procedure:
- Create an app for SharePoint using Office 365 Tools. If you have missed out on how to create an app in SharePoint 2013, then Click here.
- Create a Docment Library and name it “MyDocumentLibrary”. Refer to my article if you want to learn how to create a Document Library in SP2013.Click Here
- Create a file named “MyTextFile”. Click here if you want to learn how to create a file in a Document Library.
- Click on the Default.aspx page.

- Add 2 TextBoxes to capture a file name and file content details respectively.
- Add a button to create a file in the Document Library and place it inside the “PlaceHolderMain” tag.
Enter File Name: <input type="text" id="txtFileName" name="txtFileName"></input>
Enter File Content: <input type="text" id="txtFileContent" name="txtFileContent"/>
<button id="btnUpdateFile" onclick="updateFile()">Update File</button>
- Click on the App.js file.

- Globally declare the content, web and list objects as shown below.
var context = SP.ClientContext.get_current(); //gets the current context
var web = context.get_web(); //gets the web object
var list = web.get_lists(); //gets the list collection object
var targetList;
var fileUpdate;
var fileContent;
- Now write the function to update a file in a Document Library.
function updateFile() {
targetList = list.getByTitle("MyDocumentLibrary");
fileUpdate = new SP.FileCreationInformation();
var fileName = document.getElementById("txtFileName").value; //gets the file name
fileName = fileName + ".txt";
fileUpdate.set_url(fileName);
fileUpdate.set_content(new SP.Base64EncodedByteArray());
//set the overwrite attribute to true to update the file content.
fileUpdate.set_overwrite(true);
fileContent = document.getElementById("txtFileContent").value; //gets the file contents.
for (var content = 0; content < fileContent.length; content++) {
fileUpdate.get_content().append(fileContent.charCodeAt(content)); //add the text content to the file.
}
//update a file instance
var updateFileData = targetList.get_rootFolder().get_files().add(fileUpdate);
context.load(updateFileData);
context.executeQueryAsync(onFileUpdationSuccess, onFileUpdationSuccess);
}
- Here in this example we are updating a file in a Document Library.
- Declare a “FileCreationInformation” object.
- Set the overwrite attribute to true to update the file content using the File Creation object.
- Set the URL and content attribute using the FileCreationInformation object and then add this object to the Document Library using the add method.
- Load the updated file object and execute the code by calling executeQueryAsync ().
function onFileUpdationSuccess() {
alert("file Created");
}
function onFileUpdationFail(sender, args) {
alert('Failed to update a file. Error:' + args.get_message());
}
- That's it!! Now let's start testing.
Testing
- Now to run the app click on the "Play" button available towards the left-most corner.

- The app is packaged, deployed, and installed on your Office 365 Site.

- Now you will be able to see the following page.

- Enter the file name as “MyTextFile” and file content as “This is my updated content” in the respective text boxes and click on the “Update File” button.

- Now to check file that was updated recently Let's navigate to the following URL.
Syntax: https://yoursite/DocumentLibraryName
Example: https://mysite/MyDocumentLibrary
- You will be able to see the text file.
- Click on “…” and click on “Edit”.

- Now let's go and check the content that we updated.

Thus in this article you saw how to update a file in a Document Library in SharePoint 2013 using CSOM-JavaScript.
Humayun Kabir MamunPosted Mar 3, 2016, 2:03 AM
Nice...
ratandeep singhPosted Mar 24, 2015, 7:26 AM
Content is not getting saved. Its giving me file path in the text file .
Abhay ShankerPosted Mar 5, 2014, 1:00 AM
very good explanation in step by step
Lakshmanan Sethu SankaranarayanPosted Mar 4, 2014, 5:39 AM
Nice Article Veda. Please change the category to SharePoint 2013.