Skip to main content

How to get Data from a SharePoint list REST API using JavaScript?

Fetch Data from a SharePoint list Using JavaScript:

Just run the snippet code on you SharePoint page (Create a list Name Citizen on you SP site with required fields.columns):

<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 be =


Comments

Popular posts from this blog

How to send email using SharePoint REST API in jQuery/JavaScript?

  Send email using SharePoint REST API in jQuery/JavaScript: We can able to send email to the SharePoint user using below REST API on SharePoint Online. We need to load SP.js file in code. we must need to give valid SharePoint user to send email. We can able to send email to valid SharePoint user of same organization.      var   restAPI  =  _spPageContextInfo . webAbsoluteUrl  +  "/_api/SP.Utilities.Utility.SendEmail" ;      $ . ajax ({          contentType:   'application/json' ,          url:   restAPI ,          type:   "POST" ,          data:   JSON . stringify ({              'properties' :  {             ...

How to get all site users of SharePoint using REST API?

Get all users of SharePoint site using REST API: These users and its data are required to update people field of a SharePoint list. By using below snippet code we can able to get details of a user of SharePoint site.     var webURL = _spPageContextInfo.webAbsoluteUrl+"/_api/Web/SiteUsers";         $.ajax({           url: customFieldURL,           method: "GET",           headers: {             "Accept": "application/json; odata=verbose"           },           success: function (data) {            console.log(data);             data.d.results.forEach(function(cm){                     console.log(cm.Title+" - "+cm.Email+" - "+cm.Id);             });      ...