Hi All,
In a following post we will see how to read List Items using REST API and bind it to a Page.
Please find the below code snippet.It is a self explanatory.
<html> <head> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script type="text/javascript"> var context; var web; $(document).ready(function () { SP.SOD.executeFunc('sp.js', 'SP.ClientContext', FunctionReady); FunctionReady(); }); function FunctionReady() { context= new SP.ClientContext.get_current(); web=context.get_web(); context.load(web); context.executeQueryAsync(onRequestSuccess,onRequestFailure); } //get the current web context function onRequestSuccess() { // Get the current web Url var QueryUrl=web.get_url(); MyfirstRest(QueryUrl); } function onRequestFailure(sender,args) { alert("Error Occured:"+args.get_message()); } // My function which is used to get the List Collection . Currency is my List Name. function MyfirstRest(webUrl) { //Creating Url var queryUrl = webUrl+"/_api/lists/getbytitle('Currency')/items"; $.ajax({ url: queryUrl, method: "GET", headers: { "Accept": "application/json; odata=verbose" }, success: onQuerySuccess, error: onQueryError }); } function onQuerySuccess(data) { var r = data.d.results; //Creating a table structure $("#myResult").append('<table>') $("#myResult").append('<tr><td style="font-weight:italic">Currency</td></tr>') //Looping Jason data and binding to div $.each(r,function(index,Loaddata){ $("#myResult").append('<tr>') $("#myResult").append('<td>'+Loaddata.Title+ '</td>') $("#myResult").append('</tr>') }); $("#myResult").append('</table>') } function onQueryError(error) { alert("Error: "+error.statusText); } </script> </head> <body> <div id="myResult"> </div> </body> </html>
Final Out put will be looks as follows:
Note: As part of demo , I have place Content Editor Webpart
in my page and mapped this file with it.
Update: Instead of Writing separate function to get the current Web, We can use url=_spPageContextInfo.webAbsoluteUrl; to get the Current Web URL in the above Code.
Related Post
SharePoint 2013
- List view render using JSLink with Fabric UI
- Delete Quick Links using Powershell
- delete/Add Top Navigation Links using Powershell
- Get CheckedOut Files from SharePoint 2013 and MOSS2007
- PowerShell::Get Content Database Size in SharePoint
- PowerShell :: Delete All List Items
- Powershell :: Apply Master Page to all sub sites under site collection
- Internet Explorer cannot display this feed - while accessing REST URL in SharePoint
- SharePoint Installation: The tool was unable to install application server role web server (IIS) role SharePoint 2013.
- Basics: Difference between Synchronous Events and Asynchronous Events in SharePoint
- Remove-SPSite 0x80070003 - Forcely delete Site Collection that cannot be deleted
- This operation can be performed only on a computer that is joined to a server farm by users who have permissions in SQL Server to read from the configuration database. To connect this server to the server farm, use the SharePoint Products Configuration Wizard, located on the Start menu in Microsoft SharePoint 2010 Products.
- Restore-SPSite: The operation that you are attempting to perform cannot be completed successfully.
- Migrate Multiple site(sub sites) using MetaLogix and change the Template in Target
- Issue:The date file "C:\ProgramData\Metalogix\EnvironmentSettings.xml" contains invalid data.Values in that file reset to defaults
- Get List Field Type using ECMA Script in SP Hosted Apps
- SharePoint Interview Questions and Answers..
- Issues:SharePoint Foundation 2013 Prerequisites installation(off line) on Windows Server2012 R2
- Get Choice Field Type Values using ECMA Script
- Something Went wrong error message while deploying App in SharePoint 2013
- The Server was unable to save the form at this time. Please try again.
- Get the Items inside the Folder in document Library in SharePoint 2013 using REST API
- SharePoint Client Browser for SharePoint Online and SharePoint On-Premises
- What is Managed meta data service in SharePoint 2010, 2013 and How to Configure it?
- Deprecated features in SharePoint 2013
REST API
- Internet Explorer cannot display this feed - while accessing REST URL in SharePoint
- Get the Items inside the Folder in document Library in SharePoint 2013 using REST API
- XMLHttpRequest: Network Error 0x2eff, Could not complete the operation due to error 00002eff.
- Add Attachment to the List Item Using REST API in SharePoint
- Insert Items into List by using REST API in SharePoint 2013
Where is the Rest API in this code? This is JSOM, not REST API. Bad Impression
ReplyDelete