Introduction
In this article, we will learn how to pass SharePoint List Data to a jQuery DataTable using REST API and display SharePoint list data on SitePages using DataTable.
DataTables is a plug-in for the
jQuery Javascript library. It is a highly flexible tool, based on the foundations of progressive enhancement, and will add advanced interaction controls to any HTML table.
[Official Site] :
https://datatables.net/ - Please refer to the official site for documentation and the getting started guide
Before starting with this post, we must have some basic knowledge of the following.
- HTML Tables
- Rest API in SharePoint
- JavaScript
Implementation
Step 1
Create a Sharepoint List with data, this is the list from which we will retrieve the data and pass the data to jQuery DataTable. Skip this step if you already have the Data Source List on your SharePoint Site.
I have created a list of sample data for our project, Sample Data Source: jQuery DataTable Official Site, You can also use import spreadsheet SharePoint App to import bulk data from excel spreadsheet
Step 2
Now we will create the script file and the html Site page for displaying the list data.
- HTML file to render the data
- JS file to get the data from SharePoint list.
Go to
Data Table CDN to get the required JS and CSS files.
We need to follow CDN scripts, that are required to be used in our functionality
- jquery.min.js
- jquery.dataTables.min.js
- jquery.dataTables.min.css
- dataTables.jqueryui.min.css
Create the HTML file in a text editor (I use notepad ++)
Note
jquery.min.js should be loaded before jquery.dataTables.min.js as shown in the below code block,
- <!DOCTYPE html>
- <html>
- <head>
- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
- <script type="text/javascript" src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
-
- <script type="text/javascript" src="/SiteAssets/GetData.js"></script>
-
-
- <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css">
- <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.12/css/dataTables.jqueryui.min.css">
- </head>
- <body>
- <div>
- <table id="table_id" class="display" cellspacing="0" width="100%">
- <thead>
- <tr>
- <th>Name</th>
- <th>Position</th>
- <th>Office</th>
- <th>Age</th>
- <th>Joining Date</th>
- </tr>
- </thead>
- <tfoot>
- <tr>
- <th>Name</th>
- <th>Position</th>
- <th>Office</th>
- <th>Age</th>
- <th>Joining Date</th>
- </tr>
- </tfoot>
- </table>
- </div>
- </body>
- </html>
Step 3
Now, we will get Data from the SharePoint list. For this, write a function to get the data, using REST API.
Create the GetData.js file in a text editor (I use notepad ++)
- function loadItems() {
- var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
- var oDataUrl = siteUrl + "/_api/web/lists/getbytitle('EmployeeInfo')/items?$select=Title,Position,Office,Age,Joining_x0020_date";
- $.ajax({
- url: oDataUrl,
- type: "GET",
- dataType: "json",
- headers: {
- "accept": "application/json;odata=verbose"
- },
- success: mySuccHandler,
- error: myErrHandler
- });
- }
Note
Use $top=5000 if items more than 100.
$select filter values should be the internal names of the columns, verify the values using a browser-based GET Request [Use Internet Explorer Only] Internet Explorer Screenshot - Put API Url in the browser,
Step 4
Now we will bind the data obtained from the API Call with the jQuery DataTable [HTML View].
mySuccessHandler function binds the data to the DataTable.
- function mySuccHandler(data) {
- try {
-
- $('#table_id').DataTable({
-
- "aaData": data.d.results,
- "aoColumns": [
- {
- "mData": "Title"
- },
- {
- "mData": "Position"
- },
- {
- "mData": "Office"
- },
- {
- "mData": "Age"
- },
- {
- "mData": "Joining_x0020_date"
- }
- ]
- });
- } catch (e) {
- alert(e.message);
- }
- }
myErrHandler function
- function myErrHandler(data, errMessage) {
- alert("Error: " + errMessage);
- }
Our GetData.js file will look like,
- $(document).ready(function() {
- loadItems();
- });
-
-
- function loadItems() {
- var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
- var oDataUrl = siteUrl + "/_api/web/lists/getbytitle('EmployeeInfo')/items?$select=Title,Position,Office,Age,Joining_x0020_date";
- $.ajax({
- url: oDataUrl,
- type: "GET",
- dataType: "json",
- headers: {
- "accept": "application/json;odata=verbose"
- },
- success: mySuccHandler,
- error: myErrHandler
- });
- }
-
- function mySuccHandler(data) {
- try {
-
- $('#table_id').DataTable({
-
- "aaData": data.d.results,
- "aoColumns": [
- {
- "mData": "Title"
- },
- {
- "mData": "Position"
- },
- {
- "mData": "Office"
- },
- {
- "mData": "Age"
- },
- {
- "mData": "Joining_x0020_date"
- }
- ]
- });
- } catch (e) {
- alert(e.message);
- }
- }
-
- function myErrHandler(data, errMessage) {
- alert("Error: " + errMessage);
- }
Step 5
Copy both the files [html and JavaScript] to SharePoint SiteAssets Library.
Create a Webpart Page (Named DataTable) and store it in the pages or the site pages library.
Add a content editor to DataTable Page. Now, give HTML file reference to the content editor into Content Editor.
Click OK and save the page
Output UI
Real-Time Search, Paging, Sorting, View Functionality [In-built DataTable functionality - NO CODE]
I hope this article was helpful in getting started with the jQuery DataTable plugin. Please share your valuable suggestions and feedback.