Created
October 15, 2023 22:38
-
-
Save Zerg00s/16b86bf79f6021ebe63552d5cecc351b to your computer and use it in GitHub Desktop.
Working with SharePoint rest using REST api and browser console
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
// Get SharePoint List Fields in a table | |
function getListSchema(listName) { | |
var siteUrl = _spPageContextInfo.webAbsoluteUrl; | |
var endpointUrl = `${siteUrl}/_api/web/lists/getbytitle('${listName}')/fields?$filter=Hidden eq false`; | |
fetch(endpointUrl, { | |
method: 'GET', | |
headers: { | |
'Accept': 'application/json;odata=nometadata', | |
'Content-Type': 'application/json;odata=verbose', | |
}, | |
credentials: 'same-origin' // Needed for SharePoint On-Premise | |
}) | |
.then(response => { | |
if (!response.ok) { | |
throw new Error('Network response was not ok ' + response.statusText); | |
} | |
return response.json(); | |
}) | |
.then(data => { | |
var fields = data.value; | |
var availableFields = fields.map(field => { | |
return { | |
Title: field.Title, | |
TypeDisplayName: field.TypeDisplayName, | |
EntityPropertyName: field.EntityPropertyName, | |
Hidden: field.Hidden, | |
Required: field.Required, | |
ReadOnlyField: field.ReadOnlyField, | |
CanBeDeleted: field.CanBeDeleted, | |
SchemaXml: field.SchemaXml, | |
} | |
}); | |
renderTable(availableFields); | |
}) | |
.catch(error => { | |
console.error('There has been a problem with your fetch operation:', error); | |
}); | |
} | |
function renderTable(data) { | |
let table = ''; | |
let headers = Object.keys(data[0]); | |
table += headers.join('\t') + '\n'; | |
data.forEach(item => { | |
let row = []; | |
headers.forEach(header => { | |
row.push(item[header]); | |
}); | |
table += row.join('\t') + '\n'; | |
}); | |
console.log(table); | |
} | |
// Call the function to get the list schema | |
getListSchema('Issue tracker'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment