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 get list item entity type full name of a SharePoint list using REST API ?

REST API to get list item entity type full name: Bullet method (small and accurate): siteurl( AbsoluteUrl ) + /_api/Web/Lists/getbytitle('ListName')/ListItemEntityTypeFullName or   _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getbytitle('SPList')/items?select=EntityTypeFullName **Use this above API just replacing absolute site URL and list name **Then put this prepared URL into browser and press enter button and obtain list entity type full name of related list. **You will get ListEntityTypeFullName like " SP.Data.EmployeeListItem " **Employee(may be a list name) How it is? Is it working or not you can tell us using comment section ?

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' :  {             ...