How to Get All List Items Of A List Using REST API In SharePoint Online And Office 365


Welcome to an article on “How to Get all list items of a list using REST API in SharePoint Online and Office 365,” where we will see the steps to create an App using the Napa Tool, which will help us to get all the items of a list in a view using REST API.

  • Open the “NAPA” Office 365 development tools through your SharePoint store.

  • Click Add New Project.

  • It will ask you, what type of App do you want to build?


  • Select SharePoint Add-in and provide a name to your app and click on Create.


  • You will see the screenshot, shown below, on the app:


  • Click Default.aspx and paste the code, given below:

  1. “<asp:ContentContentPlaceHolderIDasp:ContentContentPlaceHolderID="PlaceHolderMain" runat="server">”. 

Code

  1. <div> <p>

  2. <b>List Items</b>

  3. <br />

  4. <select style="height:400px; width:200px" multiple="multiple" id="selectalllistitems">

  5. </select>

  6. </p>

  7. </div>

  • Now, on the navigation, click on the App.js file and paste the code, shown below, by removing the previous code completely.

Co

  1. 'use strict';  var hostweblink;  

  2. var applink;  

  3. // Get the links on load of the app

  4. $(document).ready(function()   

  5. {      

  6. hostweblink = decodeURIComponent(          

  7. getQueryStringParameter("SPHostUrl"));      

  8. applink = decodeURIComponent(          

  9. getQueryStringParameter("SPAppWebUrl"));  

  10. //Load the script base     

  11. var scriptlink = hostweblink + "/_layouts/15/";  

  12. //Load the JS File     

  13. $.getScript(scriptlink + "SP.RequestExecutor.js", loadPage);  });  

  14. // Retrieve the files

  15. function getQueryStringParameter(paramToRetrieve)  {      

  16. var paramsval = document.URL.split("?")[1].split("&");  

  17. for (var i = 0; i < paramsval.length; i = i + 1)      

  18. {          

  19. var singleParam = paramsval[i].split("=");  

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

  21. }  

  22. }  

  23. function loadPage()   

  24. {      

  25. getListItems();  

  26. }  

  27. //Retrieve all of the list items function getListItems()  {      

  28. var play;      

  29. play = new SP.RequestExecutor(applink);      

  30. play.executeAsync      (

  31. {          

  32. url: applink + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Custom%20List')/items?@target='" + hostweblink + "'",          

  33. method: "GET",          

  34. headers:           

  35. {  

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

  37. },          

  38. success: getitemssuccess,         

  39.  error: getitemsfailure      

  40. });  

  41. }  

  42. //get all the items on load if the list id found

  43. function getitemssuccess(data)   

  44. {      

  45. var jsonobj = JSON.parse(data.body);      

  46. var selectallitems = document.getElementById("selectalllistitems");  

  47. if (selectallitems.hasChildNodes())      

  48. {  

  49. while (selectallitems.childNodes.length >= 1)           

  50. {             

  51.  selectallitems.removeChild(selectallitems.firstChild);          

  52. }      

  53. }      

  54. var display = jsonobj.d.results;  

  55. for (var i = 0; i < display.length; i++)      

  56. {          

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

  58. selectitems.value = display[i].Title;          

  59. selectitems.innerText = display[i].Title;          

  60. selectallitems.appendChild(selectitems);      

  61. }  

  62. }  

  63. //show the reason why the code failed

  64. function getitemsfailure(data, errorCode, errorMessage)  

  65. {      

  66. alert("Problem loading the list: " + errorMessage);  

  67. }  

  • Click the settings icon on the tool on the left.

  • Under the properties, select Permissions and provide full control to the App on the Site Collection level.


  • Click the deploy button on the left and run the project.


  • Click the launch button.


  • Choose your list and accept the trust and click on ‘Trust It’.


  • Your App will be deployed and will open for you, as per the following screenshot, with all the items from the Custom List.


  • Here you can see below the list items as per your app display.


Here, we saw today how to get all the list items of a list using REST API in SharePoint Online and Office 365. You will love your App.

Recent Posts

See All