0

I am trying to pull SharePoint List Data as JSON so I can populate it to a JavaScript template library.

I have used this script to test in a SharePoint Script Editor just to make sure the JSON posts to the console, and nothing is posted to the console and no errors appear.

Here is my fetch script:

<script>
 var fullUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('WeeklyReport1')/items?$select=Team,WeekOf,OffensiveReport,DefensiveReport,SpecialTeamsReport";

const getItems = () => {
  return fetch('fullUrl')
    .then(res => res.json())
    .then(posts => console.log(posts))
}
</script>

1 Answers1

0

You have to use return fullUrl as variable name

fetch(fullUrl) 

You have used fetch('fullUrl'), which is just a string fullUrl.

Do also call your function getItems();

Your Code should look like this:

 var fullUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('WeeklyReport1')/items?$select=Team,WeekOf,OffensiveReport,DefensiveReport,SpecialTeamsReport";

const getItems = () => {
  return fetch(fullUrl)
    .then(res => res.json())
    .then(posts => console.log(posts))
}

getItems();
Imran Rafiq Rather
  • 7,677
  • 1
  • 16
  • 35
  • It doesn't return JSON –  Jan 22 '21 at 17:48
  • It is XML for some reason? –  Jan 22 '21 at 17:48
  • XML was used initially but we work with JSON as it looks like Javascript Object. You will have to find a way to convert this XML into JSON and may be do a JSON.parse(data) to work with the data ! This may help https://stackoverflow.com/questions/1773550/convert-xml-to-json-and-back-using-javascript :-) – Imran Rafiq Rather Jan 22 '21 at 20:39