Introduction

We can use the SharePoint 2013 Representational State Transfer (REST) service to do the same tasks you can do when you use .Net CSOM or JSOM.

Here I will explain how to retrieve all lists and/or libraries in SharePoint 2013 using the REST API.

Procedure

Prerequisite

These are the important steps to be done before creating the app.

Specify the following permissions that your app needs:

Expand the Scripts node, choose the App.js file and delete the contents of the file and replace with the following code:

  1. var hostweburl;
  2. var appweburl;
  3. // Get the URLs for the app web the host web URL from the query string.
  4. $(document).ready(function () {
  5. //Get the URI decoded URLs.
  6. hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
  7. appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
  8. // Load the SP.RequestExecutor.js file.
  9. $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js", runCrossDomainRequest);
  10. });
  11. // Build and send the HTTP request.
  12. function runCrossDomainRequest() {
  13. var executor = new SP.RequestExecutor(appweburl);
  14. executor.executeAsync({
  15. url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists?@target='" + hostweburl + "'",
  16. method: "GET",
  17. headers: { "Accept": "application/json; odata=verbose" },
  18. success: getlistfromsite,
  19. error: errorHandler
  20. });
  21. }
  22. //retrive all lists from site
  23. function getlistfromsite(data) {
  24. var jsonObject = JSON.parse(data.body);
  25. var oists = document.getElementById("lists");
  26. if (oists.hasChildNodes())
  27. {
  28. while (oists.childNodes.length >= 1) {
  29. oists.removeChild(oists.firstChild);
  30. }
  31. }
  32. var results = jsonObject.d.results;
  33. for (var i = 0; i < results.length; i++)
  34. {
  35. var listcombined = document.createElement("option");
  36. listcombined.value = results[i].Title;
  37. listcombined.innerText = results[i].Title;
  38. oists.appendChild(listcombined);
  39. }
  40. }
  41. //error handler
  42. function errorHandler(){
  43. alert('error');
  44. }
  45. // Get a query string value.
  46. // For production apps, you may want to use a library to handle the query string.
  47. function getQueryStringParameter(paramToRetrieve) {
  48. var params = document.URL.split("?")[1].split("&");
  49. //var strParams = "";
  50. for (var i = 0; i < params.length; i = i + 1) {
  51. var singleParam = params[i].split("=");
  52. if (singleParam[0] == paramToRetrieve) return singleParam[1];
  53. }
  54. }
Once you have done the code part run the project .

Once you have deployed the app select the Click here to launch your app in new window link and it will redirect to another page.



Click the Trust it button here.

Output

Current users followed news from my site.



I hope you have enjoyed this.