Created
April 21, 2017 13:11
-
-
Save damsleth/de94676ba5710c4fb172bef6a16609e4 to your computer and use it in GitHub Desktop.
SharePoint Get REST data with Fetch (with logging function)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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