Update a File in a Document Library in SharePoint 2013 Using CSOM-JavaScript

Introduction

This article explains how to update a file in a Document Library in SharePoint 2013 using CSOM-JavaScript.

Prerequisites

  1. Ensure you have access to the Office 365 online.
  2. Ensure the Napa Tool is available in your site.

Use the following procedure:

  1. 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.
  2. 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
  3. Create a file named “MyTextFile”. Click here if you want to learn how to create a file in a Document Library.
  4. Click on the Default.aspx page.

    aspx page

  5. Add 2 TextBoxes to capture a file name and file content details respectively.
  6. 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>
     

  7. Click on the App.js file.

    js file

  8. 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;

     

  9. 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);

        }
     

  10. Here in this example we are updating a file in a Document Library.
  11. Declare a “FileCreationInformation” object.
  12. Set the overwrite attribute to true to update the file content using the File Creation object.
  13. Set the URL and content attribute using the FileCreationInformation object and then add this object to the Document Library using the add method.
  14. 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());

        }
     

  15. That's it!! Now let's start testing.

Testing

  1. Now to run the app click on the "Play" button available towards the left-most corner.

    Play button

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

    app

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

    page

  4. 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.

    Update File

  5. Now to check file that was updated recently Let's navigate to the following URL.

    Syntax: https://yoursite/DocumentLibraryName
    Example: https://mysite/MyDocumentLibrary

    url
     
  6. You will be able to see the text file.
  7. Click on “…” and click on “Edit”.

    Edit

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

    updated
Summary

Thus in this article you saw how to update a file in a Document Library in SharePoint 2013 using CSOM-JavaScript.