How to fetch data from REST API of a SharePoint list using JavaScript Promise/fetch method ?

Fetch the data from a list using JavaScript and create a table

Just the the snippet on your SharePoint site page <html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Table</title>

    <style>
        #htmTable {
            width100%;
            displaytable;
            border-spacing2px;
            border-colorgray;
        }

        #htmTable,
        thead,
        tr,
        tbody,
        th,
        td {
            /* border: 1px solid #457; */
            border-bottom1px solid #ddd;
            border-collapsecollapse;
        }

        #htmTable th,
        td {
            padding8px 8px;
            displaytable-cell;
            text-alignleft;
            vertical-aligntop;
        }

        #htmTable thead {
            colorwhite;
            background-color#1b1819;
        }

        #htmTable tbody tr:hover {
            background-color#dddddd;
        }

        .cFlags {
            width20%;
            height25%;
            margin-left2px;
            margin-right5px;
        }

        .txtBorder {
            border-radius5px;
            height30px;
            width300px;
        }
    </style>
</head>

<body>
    <table id="htmTable">
        <thead>
            <tr>
                <th>Name</th>
                <th>City</th>
                <th>Mobile</th>
                <th>Email</th>
                <th>State</th>
            </tr>
        </thead>
        <tbody id="bodyHtm">

        </tbody>
    </table>
    <script>

        function getDataFromSPList() {
            var reqURL = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Citizens')/items";

            var xhrData = {

                method: 'GET',

                headers: {

                    'Accept': 'application/json; odata=verbose'

                },

            }



            fetch(reqURLxhrData)

                .then(function (response) {

                    if (response.ok) {

                        return response.json();

                    }

                    else {

                        throw new Error(response.json());

                    }

                }).then(function (res) {

                    console.log('success');
                    var data = res.d.results;
                    var table = document.createElement('table');
                    var tbl = '';

                    data.forEach(d => {
                        tbl += '<tr>'
                        tbl += '<td>' + d.Title + '</td>';
                        tbl += '<td>' + d.City0 + '</td>';
                        tbl += '<td>' + d.Mobile + '</td>';
                        tbl += '<td>' + d.Email + '</td>';
                        tbl += '<td>' + d.State + '</td>';
                        tbl += '</tr>'
                    });
                    document.getElementById("bodyHtm").innerHTML = tbl;
                }).catch(function (error) {

                    console.log('failure.');

                    console.log(error);

                })
        }
        getDataFromSPList();
    </script>
</body>

</html>

Output will look like


Comments

Popular posts from this blog

Get list item entity type full name of a SharePoint list using REST API

Get SharePoint list name by GUID

Get Current Web Logged In User of SharePoint Site in jQuery