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

Introduction

This article explains how to read 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 know how to create a Document Library in SP2013.Click Here
  3. Create a file by named “MyTextFile”. Click here if you want to know how to create a file in Document Library.
  4. Click on the Default.aspx page.

    aspx page

  5. Click on the App.js file.

    js file

  6. Globally declare the content and web objects as shown below.

    var context = SP.ClientContext.get_current(); //gets the current context

    var web = context.get_web(); //gets the web object
     

  7. Now write the function to read a file in a Document Library.
     

    function readFile() {

            context.load(web);

            context.executeQueryAsync(OnreadServerRelativeURL, readFileFailure);

        }

        function OnreadServerRelativeURL() {

            var fileUrl = web.get_serverRelativeUrl() + "/MyDocumentLibrary/MyTextFile.txt";

            $.ajax({

                url: fileUrl,

                type: "GET"

            })

    .done(Function.createDelegate(this, readFileSuccess))

    .error(Function.createDelegate(this, readFileFailure));

    }
     

  8. Here in this example we are reading a file in a Document Library.
  9. Load the web object to get the server realtive URL by calling executeQueryAsync ().
  10. After getting the server relative URL we need to do a GET operation on file URL using ajax.
     

        function readFileSuccess(data) {

            alert(data);

        }

        function readFileFailure(sender, args) {

            debugger;

            alert('Failed to read a file. Error:' + args.get_message());

        }
     

  11. That's it. Now let's start testing.

Testing

  1. Now to run the app click on the "Play" button that is 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. Before clicking on Read File button let's navigate to the following URL and check the content in the “MyTextFile”.

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

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

    Edit

  7. Let's check the content.

    content

  8. Now click on the Read File button.
  9. You will be able to see the alert message with the same content. That's it!

    alert message with the same content

Summary

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