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
- On your Developer Site, open the "Napa" Office 365 Development Tools and then choose Add New Project.
- Choose the App for SharePoint template, name the project Retrievelist and then choose the Create button.
Prerequisite
These are the important steps to be done before creating the app.
Specify the following permissions that your app needs:
- Choose the Properties button at the bottom of the page.
- In the Properties window, choose Permissions.
- In the Content category, set Write permissions for the Tenant scope.
- In the Social category, set Read permissions for the User Profiles scope.
- Close the Properties window.
Expand the Scripts node, choose the App.js file and delete the contents of the file and replace with the following code:
- var hostweburl;
- var appweburl;
-
-
- $(document).ready(function () {
-
- hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
- appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
-
-
- $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js", runCrossDomainRequest);
- });
-
-
- function runCrossDomainRequest() {
- var executor = new SP.RequestExecutor(appweburl);
- executor.executeAsync({
- url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists?@target='" + hostweburl + "'",
- method: "GET",
- headers: { "Accept": "application/json; odata=verbose" },
- success: getlistfromsite,
- error: errorHandler
- });
- }
-
-
-
-
- function getlistfromsite(data) {
- var jsonObject = JSON.parse(data.body);
- var oists = document.getElementById("lists");
- if (oists.hasChildNodes())
- {
- while (oists.childNodes.length >= 1) {
- oists.removeChild(oists.firstChild);
- }
- }
- var results = jsonObject.d.results;
- for (var i = 0; i < results.length; i++)
- {
- var listcombined = document.createElement("option");
- listcombined.value = results[i].Title;
- listcombined.innerText = results[i].Title;
- oists.appendChild(listcombined);
- }
- }
-
-
- function errorHandler(){
- alert('error');
- }
-
-
-
- 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];
- }
- }
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.