How to read file content using JavaScript object model jsom in SharePoint Online Office 365?

read file content using JavaScript object model jsom in SharePoint Online Office 365
InfoPath alternatives for form designing SharePoint

Here we will discuss how we can read all file content using javascript object model (jsom) in SharePoint online office 365. This code works fine for SharePoint 2013 as well as in SharePoint 2016. If you want to know how we can create a file using javascript object model in SharePoint online, you can see this article.

Also you can read some useful articles:
– Insert item to SharePoint list using JavaScript Object Model JSOM in SharePoint Online

– Retrieve all site templates using PowerShell and retrieve site templates using SharePoint 2016 server object model

– Activate Group Work Site Template in SharePoint 2013

Here we have taken a button, on click on that button we will read the content of the document and will display in a div. Here we will write both html and javascript object model code inside a script editor web part which we will add inside a web part page.

HTML Code:

<input type=”button” id=”btnSubmit” value=”Read Document” /><br />

<div id=”divReadDocument” />

Jsom Code:

Below is the JSOM code:

<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js”></script>

<script>

$(function () {

bindButtonClick();

});

function bindButtonClick() {

$(“#btnSubmit”).on(“click”, function () {

readDocument();

});

}

function readDocument() {

var fullUrl = _spPageContextInfo.webAbsoluteUrl + “/Shared Documents/Bijay.txt”;

$.ajax({

url: fullUrl,

type: “GET”

})

.done(function (data) {

$(“#divReadDocument”).html(data);

})

.fail(function () {

alert(“error”);

});

}

</script>

Once you will Save the page and click on the button, you can see the .txt file content like below:

read file content using JavaScript object model jsom in SharePoint Online Office 365
read file content using JavaScript object model jsom in SharePoint Online Office 365

Hope this will be helpful.

Similar SharePoint 2013 Tutorials

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →

Leave a Reply