0

I am trying to use REST to get data from inside a SharePoint list. The HTML/ASPX page that makes the request resides in the site assets library. It returns nothing. Any idea why? Please see below:

    <!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

    <button onclick="GetListItems();" type="button">Get All List Items​</button>

    <script>
    function GetListItems()
    {
    var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Request')/items";
    $.ajax({
    url: url,
    type: "GET",
    headers:
     {
    "accept":"application/json; odata=verbose"
    },
    success: onSuccess,error: onError
    });
    }

    function onSuccess(data) {
    var items = data.d.results;
    var allItems='';
    for (var i = 0; i < items.length; i++) {
    allItems+="Item ID- "+ items[i].Id+ " : Title- " + items[i].Title + '\r\n';
    }
    alert(allItems);
    }

    function onError(error) {alert(JSON.stringify(error));
    }
    </script>
    <title>Resttest</title>

  </head>
  <body>
  </body>
</html>

1 Answer 1

0

This type of JavaScript injection is not natively supported in SharePoint Online Modern pages. Something similar can be used in SharePoint Online Classic pages using the Content Editor or Script Editor web parts.

For example, you could update the content of your file to something like that shown below, and then inject the HTML and script into a Web Part Page using a Content Editor web part.

<div>
    <button onclick="GetListItems();" type="button">Get All List Items</button>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script>
    function GetListItems() {
        var url = _spPageContextInfo.webAbsoluteUrl +
            "/_api/web/lists/getByTitle('Request')/items";
        $.ajax({
            url: url,
            type: "GET",
            headers:
            {
                "accept": "application/json; odata=verbose"
            },
            success: onSuccess, error: onError
        });
    }

    function onSuccess(data) {
        var items = data.d.results;
        var allItems = '';
        for (var i = 0; i < items.length; i++) {
            allItems += "Item ID- " + items[i].Id + " : Title- " + 
                items[i].Title + '\r\n';
        }
        alert(allItems);
    }

    function onError(error) {
        alert(JSON.stringify(error));
    }
</script>

The supported way to do something similar in a SharePoint Online Modern page is to use the SharePoint Framework (SPFx). If you really want to use the JavaScript injection technique shown above in SharePoint Modern pages you could use the SPFx Script Editor web part built by Mikael Svenson. See Perhaps the only SharePoint Framework web part you will ever need for more information.

I cover the topic of JavaScript injection in SharePoint Online in detail in this YouTube video: Working with JavaScript Injection in the SharePoint Content Editor Web Part

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.