We'll learn how to call Rest Api in SharePoint Online, this is the requirment:
There is a customer list named "Customers", we'll output the title of all items in the list.
Introduce Rest API:
The request examples in this article assume, you ' re using the Cross-domain library (SP. Requestexecutor.js) to make Cross-domain requests, so they useSP. Appcontextsite in the endpoint URI. See how to:access SharePoint-data from apps using the Cross-domain library for more information.
Note:make sure implement the Sp.requestextcutor.js in page.
Implement in detail:
1. Create a custom list
2. New a project in Napa, add a page and implement the JS
' Use strict ', var currentcontext;var hostcontext;var Hostweb;var Hosturl;var appurl; (function () {//This code runs when the DOM is ready and creates a context object which are//needed to use the SharePoint object model$ (document). Ready (function () {Hosturl = decodeURIComponent (Getquerystringparameter ("Sphosturl")); currentcontext = new SP. Clientcontext.get_current (); hostcontext = new SP. Appcontextsite (CurrentContext, hosturl); hostweb = Hostcontext.get_web (); AppUrl = decodeURIComponent (Getquerystringparameter ("Spappweburl")); GetListItems ();}); function GetListItems () {var executor = new SP. Requestexecutor (AppUrl); Executor.executeasync ({Url:appurl + "/_api/sp. Appcontextsite (@target)/web/lists/getbytitle (' Customers ')/[email protected]= ' "+ Hosturl +" ' ", Method:" POST ", Body: ' {' query ': {' __metadata ': {' type ': ' SP '. Camlquery '}, ' ViewXml ': ' <View><Query><Where></Where></Query></View> '} ', Headers: {"Accept": "Application/json; Odata=verbose ", "Content-type": "Application/json; Odata=verbose "},success:function (data, req, text) {Console.log (Json.parse (data.body)); var result = Json.parse ( data.body). d.results;for (var i = result.length-1; I >= 0; i--) {$ (". Maincontainer"). Append ("<div>" + result[i] . Title + "</div>");}},error:function (data, req, text) {console.log (data);}});}) ();
3. Debug the project, you'll see the result
More:lists and list items REST API reference
Rest Api How to retrieve list items from SharePoint Online