Skip to content

Instantly share code, notes, and snippets.

@damsleth
Created April 21, 2017 13:11
Show Gist options
  • Save damsleth/de94676ba5710c4fb172bef6a16609e4 to your computer and use it in GitHub Desktop.
Save damsleth/de94676ba5710c4fb172bef6a16609e4 to your computer and use it in GitHub Desktop.
SharePoint Get REST data with Fetch (with logging function)
const getRestData = endpoint =>
fetch(`${_spPageContextInfo.webAbsoluteUrl}/_api/web/${endpoint}`,
{ credentials: "include", headers: { "accept": "application/json;odata=verbose" } })
.then(res => res.json().then(data => {
return data.d.hasOwnProperty("results") ? data.d.results : data.d }))
.catch(err => console.log(err));
const getListItems = listTitle => { return getRestData(`lists/getbytitle('${listTitle}')/items`) };
const logProps = (endpoint, prop) =>
getRestData(endpoint).then(d => {
let log = str => console.log(str);
if (prop) if (d.length) for (i in d) log(d[i][prop])
else log(d[prop])
else log(d);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment