Ensure appropriate permission is provided to access the content. Click on Properties button, and then click on Permissions. Set the required permission to access the content.
'use strict';
var hostweburl;
var appweburl;
// Load the required SharePoint libraries.
$(document).ready(function () {
//Get the URI decoded URLs.
hostweburl = decodeURIComponent(
getQueryStringParameter("SPHostUrl"));
appweburl = decodeURIComponent(
getQueryStringParameter("SPAppWebUrl"));
// Resources are in URLs in the form:
// web_url/_layouts/15/resource
var scriptbase = hostweburl + "/_layouts/15/";
// Load the js file and continue to load the page with information about the list content types.
// SP.RequestExecutor.js to make cross-domain requests
$.getScript(scriptbase + "SP.RequestExecutor.js", loadPage);
});
//Utilities
// Retrieve a query string value.
// For production purposes you may want to use a library to handle the query string.
function getQueryStringParameter(paramToRetrieve) {
var params = document.URL.split("?")[1].split("&");
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == paramToRetrieve) return singleParam[1];
}
}
function loadPage() {
getListContentTypes();
}
//Retrieve all of the list content types
function getListContentTypes() {
var executor;
// Initialize the RequestExecutor with the app web URL.
executor = new SP.RequestExecutor(appweburl);
executor.executeAsync({
url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Custom List')/contenttypes?@target='" + hostweburl + "'",
method: "GET",
headers: {
"Accept": "application/json; odata=verbose"
},
success: getListContentTypesSuccessHandler,
error: getListContentTypesErrorHandler
});
}
//Populate the selectColumns control after retrieving all of the list content types.
function getListContentTypesSuccessHandler(data) {
var jsonObject = JSON.parse(data.body);
var selectContentTypes = document.getElementById("selectContentTypes");
if (selectContentTypes.hasChildNodes()) {
while (selectContentTypes.childNodes.length >= 1) {
selectContentTypes.removeChild(selectContentTypes.firstChild);
}
}
var results = jsonObject.d.results;
for (var i = 0; i < results.length; i++) {
var selectOption = document.createElement("option");
selectOption.value = results[i].Name;
selectOption.innerText = results[i].Name;
selectContentTypes.appendChild(selectOption);
}
}
function getListContentTypesErrorHandler(data, errorCode, errorMessage) {
alert("Could not get List Content Types: " + errorMessage);
}
Thus in this article you saw how to get all the content types for a specific list using the REST API in SharePoint 2013 Online.