Export list items to Excel using JavaScript object model (jsom) in SharePoint 2013

This jsom SharePoint tutorial, we will discuss how to export SharePoint list items to excel using the javascript object model (jsom) in SharePoint Online or SharePoint 2013/2016.

Export list items to Excel using JavaScript object model in SharePoint 2013/2016/Online

I have a list name as MyTestList, we are retrieving and displaying the list items in a table. And in another button, we are exporting list items to an excel in SharePoint.

The limitation of this method is export to excel will work in IE because it requires ActiveX.

So to do this Put the below code in a single Script editor web part in a web part page in SharePoint Online or SharePoint 2013/2016.

Below is the code to display items into the table in SharePoint:

<script language="javascript" type="text/javascript">
function retrieveListItemsInclude() {
var table = document.getElementById('cntryTable');
while (table.rows.length > 1) {
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('MyTestList');
var camlQuery = new SP.CamlQuery();
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem, 'Include(Title)');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed)); }
function onQuerySucceeded(sender, args) {
var listItemInfo = ";
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var table = document.getElementById('cntryTable');
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
cell1.innerHTML = oListItem.get_item('Title');
} }
function onQueryFailed(sender, args) { alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); } </script>
<table id="cntryTable" border="1″>

<input type='button' value='Display List Items' onclick="retrieveListItemsInclude();"/>

Below is the code to export the data to excel in SharePoint.

<script language="javascript" type="text/javascript">
function generateexcel() {
var x = document.getElementById('cntryTable').rows;
var xls = new ActiveXObject(“Excel.Application");
xls.visible = true
for (i = 0; i < x.length; i++) {
var y = x[i].cells;
for (j = 0; j < y.length; j++) {
xls.cells(i + 1, j + 1).value = y[j].innerText;
<input type='button' value='Export To Excel' onclick="generateexcel();"/>

It will display like this in the page:

sharepoint export to excel javascript
sharepoint export to excel javascript

You may like following jsom SharePoint tutorials:

Hope this SharePoint tutorial explains, how to export list items to Excel using JavaScript object model in SharePoint 2013/2016 or SharePoint Online.

  • >