0

I have created a list in sharepoint and trying to load in datatable using rest api with search and export option to tranfer in excel file for download. kindly help.

1
  • You want to load SharePoint list data in datatable datatables.net ? If you just want to export in excel then you can use out of the box ways: support.office.com/en-us/article/… , or use the 'Get data' option in Excel to load it directly from SharePoint online list. Any other reason for using DataTable? Commented May 17, 2020 at 15:38

1 Answer 1

1

Here is a demo to bind Rest Json to DataTable and with export option for your reference:

<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script  type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script  type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.1/js/dataTables.buttons.min.js"></script>
<script  type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.1/js/buttons.flash.min.js"></script>
<script  type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script  type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
<script  type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
<script  type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.1/js/buttons.html5.min.js"></script>
<script  type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
<script  type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.1/js/buttons.print.min.js"></script>
<link  rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css" />
<table id="requests" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Title</th>
            <th>Countries</th>
            <th>Cities</th>
        </tr>
    </thead>
</table>
<script type="text/javascript">

$(document).ready(function() {
    $('#requests').DataTable({
        'ajax': {
        'url': "https://Tenant.sharepoint.com/_api/web/lists/getbytitle('MyList')/items",
        'headers': { 'Accept': 'application/json;odata=nometadata' },
        'dataSrc': function(data) {
            console.log(data);
            return data.value.map(function(item) {
                return [
                    item.Title,
                    item.Countries,
                    item.Cities
                    ];
            });
        }
    },
       dom: 'Bfrtip',
        buttons: [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ]
    });
});
</script>

enter image description here

enter image description here

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.