top of page
Writer's pictureVijai Anand Ramalingam

How to Get All the List Templates in SharePoint 2013 Online Using REST API


Introduction

SharePoint 2013 introduces a Representational State Transfer (REST) service that is comparable to the existing SharePoint client object models. This allows the developers to interact remotely with SharePoint data using any technology that supports REST web requests. This means that developers can perform 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 list templates from the host site using REST API.


Endpoint URI:

http://sitename/_api/web/listtemplates

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:

  • Navigate to the SharePoint 2013 Online site.

  • Click on Site Contents in the quick launch bar.

  • Click on “Napa” Office 365 Development Tools.


  • Click on Add New Project.


  • 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 Properties button, and then click on Permissions. Set the required permission to access the content.



Default.aspx

Replace the contents of 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>  </asp:Content>  

  10. <%-- The markup in the following Content element will be placed in the TitleArea of the page --%>  

  11. <asp:Content ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">Page Title</asp:Content>  

  12. <%-- The markup and script in the following Content element will be placed in the <body>of the page --%>  

  13. <asp:Content ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">REST API Examples</asp:Content>  

  14. <%-- The markup and script in the following Content element will be placed in the <body>of the page --%>  

  15. <asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">  

  16. <div>  

  17. <p>  

  18. <b>List Templates</b>  

  19. <br />  

  20. <select style="height:300px; width:250px" multiple="multiple" id="selectListTemplates">

  21. </select>  

  22. </p>  

  23. </div>  

  24. </asp:Content>  


App.js

Replace the contents of App.js with the following:

  1. 'use strict';

  2. var hostweburl;

  3. var appweburl;


  4. // Load the required SharePoint libraries.

  5. $(document).ready(function () {


  6. //Get the URI decoded URLs.

  7. hostweburl = decodeURIComponent(

  8. getQueryStringParameter("SPHostUrl"));

  9. appweburl = decodeURIComponent(

  10. getQueryStringParameter("SPAppWebUrl"));


  11. // Resources are in URLs in the form:

  12. // web_url/_layouts/15/resource

  13. var scriptbase = hostweburl + "/_layouts/15/";


  14. // Load the js file and continue to load the page with information about the list templates.

  15. // SP.RequestExecutor.js to make cross-domain requests   

  16. $.getScript(scriptbase + "SP.RequestExecutor.js", loadPage);

  17. });


  18. //Utilities

  19. // Retrieve a query string value.


  20. // For production purposes you may want to use a library to handle the query string.

  21. function getQueryStringParameter(paramToRetrieve) {

  22. var params = document.URL.split("?")[1].split("&");

  23. for (var i = 0; i < params.length; i = i + 1) {

  24. var singleParam = params[i].split("=");

  25. if (singleParam[0] == paramToRetrieve) return singleParam[1];

  26. }

  27. }

  28. function loadPage() {

  29. getListTemplates();

  30. }

  31. //Retrieve all of the list templates

  32. function getListTemplates() {

  33. var executor;


  34. // Initialize the RequestExecutor with the app web URL.   

  35. executor = new SP.RequestExecutor(appweburl);   

  36. executor.executeAsync({      

  37. url: appweburl + "/_api/SP.AppContextSite(@target)/web/listtemplates @target='" + hostweburl + "'",       

  38. method: "GET",      

  39. headers: {

  40. "Accept": "application/json; odata=verbose"       

  41.  },       

  42. success: getListTemplatesSuccessHandler,        

  43. error: getListTemplatesErrorHandler    

  44. });

  45. }


  46. //Populate the selectListTemplates control after retrieving all of the list templates.

  47. function getListTemplatesSuccessHandler(data) {

  48. var jsonObject = JSON.parse(data.body);

  49. var selectListTemplates = document.getElementById("selectListTemplates");

  50. if (selectListTemplates.hasChildNodes()) {

  51. while (selectListTemplates.childNodes.length >= 1) {            

  52. selectListTemplates.removeChild(selectListTemplates.firstChild);        

  53. }   

  54.  }

  55. var results = jsonObject.d.results;

  56. for (var i = 0; i < results.length; i++) {

  57. var selectOption = document.createElement("option");

  58. selectOption.value = results[i].Name;

  59. selectOption.innerText = results[i].Name;

  60. selectListTemplates.appendChild(selectOption);

  61. }

  62. }

  63. function getListTemplatesErrorHandler(data, errorCode, errorMessage) {    

  64. alert("Could not get List Templates: " + errorMessage);

  65. }


Deploy the App

  • Click on Run Project.


  • The app will be packaged, deployed and launched.



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


  • Click on Trust it.


  • All the list templates from the host site are displayed.


Summary

Thus in this article you saw how to get all the list templates using the REST API in SharePoint 2013 Online.

0 comments

Comments


bottom of page