SharePoint List to JQuery Table

Source Code Download Link 

We have lot of benefits using JQuery Table in SharePoint such as quick search, selecting of number of items in view, shorting and footer navigation. In this SharePoint Add-in, I’m retrieving SharePoint list data and building HTML table, then the HTML table mapped with JQuery Table script. Read further for detailed step by step instruction to develop this SharePoint Add-In.

Create a new project in visual studio and select “App for SharePoint” project Template, in the new project wizard select SharePoint Hosted and select SharePoint environment type.

After the project is created, open Solution explorer, in that open App.js under Scripts folder and remove all default code which is generated by visual studio and paste the below Java script code into that page.

var context;
var hostweburl;
var appweburl;
var appContextSite;
var list;
var listItems;
var web;
 
$(document).ready(function () {
    //SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getUrl);
    getUrl();
});
 
function getUrl() {
    hostweburl = getQueryStringParameter("SPHostUrl");
    appweburl = getQueryStringParameter("SPAppWebUrl");
    hostweburl = decodeURIComponent(hostweburl);
    appweburl = decodeURIComponent(appweburl);
    var scriptbase = hostweburl + "/_layouts/15/";
    $.getScript(scriptbase + "SP.Runtime.js",
    function () {
        $.getScript(scriptbase + "SP.js",
        function () { $.getScript(scriptbase + "SP.RequestExecutor.js", execOperation); }
        );
    }
    );
    event.preventDefault();
}
 
function execOperation() {
    context = new  SP.ClientContext(appweburl);
    var factory =
    new SP.ProxyWebRequestExecutorFactory(
    appweburl
    );
    context.set_webRequestExecutorFactory(factory);
    appContextSite = new  SP.AppContextSite(context, hostweburl);
    web = appContextSite.get_web();
    context.load(web);
    var camlQuery = new SP.CamlQuery();
    list = web.get_lists().getByTitle("Documents");
    listItems = list.getItems(camlQuery);
    context.load(list);
    context.load(listItems);
    context.executeQueryAsync(onGetSPListSuccess, onGetSPListFail);
}
function onGetSPListSuccess() {
    $("#DivSPGrid").empty();
    var listInfo = '';
    var listEnumerator = listItems.getEnumerator();
    listInfo += "<table id='SPTable' class='display'><thead><tr>" +
        "<th>Id</th>" +
        "<th>Title</th>" +
        "<th>Modified By</th>" +
        "<th>Modified date</th>" +
        "</tr></thead><tbody>";
    while (listEnumerator.moveNext()) {
        var listItem = listEnumerator.get_current();
        listInfo += '<tr><td>' + listItem.get_item('ID') + '</td>'
        + '<td>' + listItem.get_item('FileLeafRef') + '</td>'
        + '<td>' + listItem.get_item('Editor').get_lookupValue() + '</td>'
        + '<td>' + listItem.get_item('Modified').format('dd MMM yyyy, hh:ss') + '</td>'
        + '</tr>';
    }
    listInfo += '</tbody></table>';
    $("#DivSPGrid").html(listInfo);
    $('#SPTable').dataTable();
}
 
// This function is executed if the above call fails
function onGetSPListFail(sender, args) {
    alert('Failed to get SP List. Error:' + args.get_message());
}
function getQueryStringParameter(paramToRetrieve) {
    var params =
    document.URL.split("?")[1].split("&");
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == paramToRetrieve)
            return singleParam[1];
    }
}

Rename default.aspx page, which is located under pages folder based on your requirement here I have changed as JQueryTable.aspx.

Double click AppManifest.xml file and select List Read permission under Permissions tab.

Add JS file, CSS file and Images into respective folders, for reference see the below image. You can find the link to download the Full project at the end of this post.

Add newly added JS and CSS files reference in the JQueryTable.aspx and add a new element of Div with id as DivSPGrid refer the below code for more detail.

<%-- The following 4 lines are ASP.NET directives needed when using SharePoint components --%>
 
<%@ Page Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" MasterPageFile="~masterurl/default.master" Language="C#" %>
 
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
 
<%-- The markup and script in the following Content element will be placed in the <head> of the page --%>
<asp:Content ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
    <script type="text/javascript" src="../Scripts/jquery-1.9.1.min.js"></script>
    <SharePoint:ScriptLink name="sp.js" runat="server" OnDemand="true" LoadAfterUI="true" Localizable="false" />
    <meta name="WebPartPageExpansion" content="full" />
 
    <!-- Add your CSS styles to the following file -->
    <link rel="Stylesheet" type="text/css" href="../Content/App.css" />
    <link href="../Content/jquery.dataTables.css" rel="stylesheet" />
    <!-- Add your JavaScript to the following file -->
    <script type="text/javascript" src="../Scripts/App.js"></script>
    <script type="text/javascript" src="../Scripts/jquery.dataTables.js"></script>
</asp:Content>
 
<%-- The markup in the following Content element will be placed in the TitleArea of the page --%>
<asp:Content ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">
    SharePoint List to JQuery Table
</asp:Content>
 
<%-- The markup and script in the following Content element will be placed in the <body> of the page --%>
<asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
 
    <div id="DivSPGrid">
        
    </div>
 
</asp:Content>

Its time to review our SharePoint Add-in output, build and deploy the project and select “Document” in the SharePoint permission request page, you can change different list from the app.js file code as per your requirement.

Feel free to contact me thru comments if you face any issues.

Good Luck on building your code!!!

Output of our JQuery Table in SharePoint

Actual SharePoint List for your reference

Source Code Download Link