How to Get the List Content Types in SharePoint 2013 Online Using REST API

In this article you will see how to get all the content types for a specific list using the REST API in SharePoint 2013 Online.

Introduction

SharePoint 2013 introduces a Representational State Transfer (REST) service that is comparable to the existing SharePoint client object models. This allows developers to interact remotely with SharePoint data using any technology that supports REST web requests. This means that developers can do Create, Read, Update and Delete (CRUD) operations from their apps for SharePoint, solutions, and client applications, using REST web technologies and standard Open Data Protocol (OData) syntax. In this article you will see the following.

  • Create an app using NAPA Tool in SharePoint 2013 Online.
  • Cross-Domain Requests.
  • Get all the content types for a specific list from the host site using REST API.

Endpoint URI

http://sitename/_api/web/lists/getbytitle(‘Custom List’)/contenttypes

Note: If you are making cross-domain requests then you need to add SP.AppContextSite(@target) and ?@target='<host web url> 'to the endpoint URI.

HTTP Request

GET: Read a Resource.

Use the following procedure to create an app using the NAPA Tool:

  1. Navigate to the SharePoint 2013 Online site.
  2. Click on Site Contents in the quick launch bar.
  3. Click on “Napa” Office 365 Development Tools.



  4. Click on Add New Project.



  5. Select App for SharePoint, enter the Project name and then click on Create.


Permissions

Ensure appropriate permission is provided to access the content. Click on the Properties button then click on Permissions. Set the required permission to access the content.



Default.aspx

Replace Default.aspx with the following:

  1. <%-- The markup and script in the following Content element will be placed in the <head>of the page --%>  
  2. <asp:Content ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">  
  3. <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js"></script>  
  4. <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>  
  5. <script type="text/javascript" src="/_layouts/15/sp.js"></script>  
  6. <!-- Add your CSS styles to the following file -->  
  7. <link rel="Stylesheet" type="text/css" href="../Content/App.css" />  
  8. <!-- Add your JavaScript to the following file -->  
  9. <script type="text/javascript" src="../Scripts/App.js"></script>  
  10. </asp:Content>  
  11. <%-- The markup in the following Content element will be placed in the TitleArea of the page --%>  
  12. <asp:Content ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">Page Title</asp:Content>  
  13. <%-- The markup and script in the following Content element will be placed in the <body>of the page --%>  
  14. <asp:Content ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">REST API Examples</asp:Content>  
  15. <%-- The markup and script in the following Content element will be placed in the <body>of the page --%>  
  16. <asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">  
  17. <div>  
  18. <p>  
  19. <b>List Content Types</b>  
  20. <br />  
  21. <select style="height:300px; width:250px" multiple="multiple" id="selectContentTypes"></select>  
  22. </p>  
  23. </div>  
  24. </asp:Content>  

App.js

Replace App.js with the following:

  1. 'use strict';  
  2.   
  3. var hostweburl;  
  4. var appweburl;  
  5.   
  6. // Load the required SharePoint libraries.  
  7. $(document).ready(function() {  
  8.   
  9. //Get the URI decoded URLs.  
  10. hostweburl = decodeURIComponent(  
  11. getQueryStringParameter("SPHostUrl"));  
  12. appweburl = decodeURIComponent(  
  13. getQueryStringParameter("SPAppWebUrl"));  
  14.   
  15. // Resources are in URLs in the form:  
  16. // web_url/_layouts/15/resource  
  17. var scriptbase = hostweburl + "/_layouts/15/";  
  18.   
  19. // Load the js file and continue to load the page with information about the list content types.  
  20. // SP.RequestExecutor.js to make cross-domain requests  
  21. $.getScript(scriptbase + "SP.RequestExecutor.js", loadPage);  
  22. });  
  23.   
  24. //Utilities  
  25. // Retrieve a query string value.  
  26. // For production purposes you may want to use a library to handle the query string.  
  27. function getQueryStringParameter(paramToRetrieve) {  
  28. var params = document.URL.split("?")[1].split("&");  
  29. for (var i = 0; i < params.length; i = i + 1) {  
  30. var singleParam = params[i].split("=");  
  31. if (singleParam[0] == paramToRetrieve) return singleParam[1];  
  32. }  
  33. }  
  34. function loadPage() {  
  35. getListContentTypes();  
  36. }  
  37. //Retrieve all of the list content types  
  38. function getListContentTypes() {  
  39. var executor;  
  40. // Initialize the RequestExecutor with the app web URL.  
  41. executor = new SP.RequestExecutor(appweburl);  
  42. executor.executeAsync({  
  43. url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Custom List')/contenttypes?@target='" + hostweburl + "'",  
  44. method: "GET",  
  45. headers: {  
  46. "Accept""application/json; odata=verbose"  
  47. },  
  48. success: getListContentTypesSuccessHandler,  
  49. error: getListContentTypesErrorHandler  
  50. });  
  51. }  
  52. //Populate the selectColumns control after retrieving all of the list content types.  
  53. function getListContentTypesSuccessHandler(data) {  
  54. var jsonObject = JSON.parse(data.body);  
  55. var selectContentTypes = document.getElementById("selectContentTypes");  
  56. if (selectContentTypes.hasChildNodes()) {  
  57. while (selectContentTypes.childNodes.length >= 1) {  
  58. selectContentTypes.removeChild(selectContentTypes.firstChild);  
  59. }  
  60. }  
  61. var results = jsonObject.d.results;  
  62. for (var i = 0; i < results.length; i++) {  
  63. var selectOption = document.createElement("option");  
  64. selectOption.value = results[i].Name;  
  65. selectOption.innerText = results[i].Name;  
  66. selectContentTypes.appendChild(selectOption);  
  67. }  
  68. }  
  69. function getListContentTypesErrorHandler(data, errorCode, errorMessage) {  
  70. alert("Could not get List Content Types: " + errorMessage);  
  71. }  
Deploy the App

 

  1. Click on Run Project.



  2. The app will be packaged, deployed and launched.



  3. Click on “Click here to launch your app in a new window”.



  4. Click on Trust it.



  5. All the content types for a specific list from host site are displayed.


Summary

Thus in this article you have seen how to get all the content types for a specific list using the REST API in SharePoint 2013 Online.