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? – UBK May 17 '20 at 15:38
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.