{"id":82,"date":"2018-04-23T18:17:00","date_gmt":"2018-04-23T18:17:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/2018\/04\/23\/how-to-fetch-sharepoint-list-using-angularjs-and-display-on-html-page-explaining-the-codes\/"},"modified":"2020-07-25T22:42:54","modified_gmt":"2020-07-25T20:42:54","slug":"how-to-fetch-sharepoint-list-using-angularjs-and-display-on-html-page-explaining-the-codes","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/how-to-fetch-sharepoint-list-using-angularjs-and-display-on-html-page-explaining-the-codes\/","title":{"rendered":"How to Fetch SharePoint List using AngularJS and Display on html Page (Explaining the Codes)"},"content":{"rendered":"<p>I am Kindson and today, I&#8217;m going to explain to you how to fetch Sharepoint using AngularJS list and display it in html page. I would try to be very clear and easy to follow in this lesson.<br \/>\nSo Let&#8217;s go!<\/p>\n<p>&nbsp;<\/p>\n<p><b>Content<\/b><\/p>\n<ol>\n<li><a href=\"https:\/\/kindsonthegenius.com\/blog\/how-to-fetch-sharepoint-list-using-angularjs-and-display-on-html-page-explaining-the-codes#t1\">Introduction<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/blog\/how-to-fetch-sharepoint-list-using-angularjs-and-display-on-html-page-explaining-the-codes#t2\">The Building Blocks You Need<\/a><\/li>\n<li><a href=\"#t3\">Understanding the Pieces of the Your Script<\/a>\n<ul>\n<li>Initializing the AngularJS App and Controller<\/li>\n<li>The GetList() Function<\/li>\n<li>The $.ajax() Method<\/li>\n<li>Success Function<\/li>\n<li>Error Function<\/li>\n<\/ul>\n<\/li>\n<li><a href=\"#t4\">Understanding the Pieces of the your html page<\/a>\n<ol>\n<li>The ng-app directive<\/li>\n<li>The ng-controller directive<\/li>\n<li>The ng-repeat directive<\/li>\n<li>Specifying Repeat Items<\/li>\n<\/ol>\n<\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/blog\/how-to-fetch-sharepoint-list-using-angularjs-and-display-on-html-page-explaining-the-codes#t5\">Final Notes<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h3 id=\"t1\">1. Introduction<\/h3>\n<p>In this simple tutorial, you I explain very clearly and in a simple way the ingredients you need to query a SharePoint list and display the content on a html page.<\/p>\n<p>Note that we can perform SELECT, INSERT, UPDATE and DELETE operations on a SharePoint list using AngularJS. In this lesson, we would examine the select operation and in the following lessons we would learn about the other CRUD operations.<\/p>\n<p>How it works is that AngulaJS makes a REST GET request to the SharePoint list and gets a JSON response. It then parses this response and displays it on a page.<br \/>\nEasy? Right? Let&#8217;s see! \u263a<\/p>\n<p>&nbsp;<\/p>\n<h3 id=\"t2\">2. Building Block you Need<\/h3>\n<p>1. You Need two\u00a0 AngularJS files: the <em>angular.js<\/em> file and the angular-route.js file<br \/>\n2. A html Page to display your results<br \/>\n3. The Script code we would write. In this case, we would include the script in the html file using the &lt;script&gt;&lt;\/script tag. But for very large scripts, a separate file would be needed<\/p>\n<p>&nbsp;<\/p>\n<h3 id=\"t3\">3. Understanding the Pieces of the Your Script<\/h3>\n<p>Let&#8217;s start writing the script and as we write, we would be explaining each piece of the script. So now, create a html file\u00a0 with the basic content:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #007700;\">&lt;html&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;head&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">type=<\/span><span style=\"background-color: #fff0f0;\">\"text\/javascript\"<\/span> <span style=\"color: #0000cc;\">src=<\/span><span style=\"background-color: #fff0f0;\">\"angular.1.6.2.js\"<\/span><span style=\"color: #007700;\">&gt;&lt;\/script&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">type=<\/span><span style=\"background-color: #fff0f0;\">\"text\/javascript\"<\/span> <span style=\"color: #0000cc;\">src=<\/span><span style=\"background-color: #fff0f0;\">\"angular-route.1.6.2.js\"<\/span><span style=\"color: #007700;\">&gt;&lt;\/script&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;title&gt;<\/span> Fetch HTML List Using AngularJS<span style=\"color: #007700;\">&lt;\/title&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/head&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;body&gt;<\/span>\r\n\r\n<span style=\"color: #007700;\">&lt;script&gt;<\/span>\r\nAngularJS Code goes here\r\n<span style=\"color: #007700;\">&lt;\/script&gt;<\/span>\r\n\r\n<span style=\"color: #007700;\">&lt;table&gt;<\/span>\r\n...\r\nAngularJS+HTML is used to display list of items in tabular form\r\n...\r\n<span style=\"color: #007700;\">&lt;\/table&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/body&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/html&gt;<\/span>\r\n<\/pre>\n<p><b>Listing 1.0: <\/b>How AngularJS works in HTML page<br \/>\n&nbsp;<\/p>\n<p>From the html file, we would write our AngularJS codes inside the &lt;script&gt;&lt;\/script&gt; part.<br \/>\nThe we would\u00a0 write the html codes inside the &lt;table&gt;&lt;\/table&gt; tag to display the data.<\/p>\n<p>&nbsp;<\/p>\n<p><b>Initializing the AngularJS App and Controller<\/b><\/p>\n<p>Write this code inside the script block<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"line-height: 125%; margin: 0;\">        <span style=\"color: #008800; font-weight: bold;\">var<\/span> appVar <span style=\"color: #333333;\">=<\/span> angular.module(<span style=\"background-color: #fff0f0;\">'listApp'<\/span>, [<span style=\"background-color: #fff0f0;\">'ngRoute'<\/span>]);\r\nappVar.controller(<span style=\"background-color: #fff0f0;\">\"controller1\"<\/span>, <span style=\"color: #008800; font-weight: bold;\">function<\/span>($scope){\r\nGetListItems($scope, <span style=\"background-color: #fff0f0;\">\"Users\"<\/span>);\r\n});\r\n<\/pre>\n<p><b>Listing 1.1: <\/b>Initializing AngularJS App and Controller<\/p>\n<p>&nbsp;<\/p>\n<p>The first line initializes and AngularJS app called appVar. This is done using angular.module method. This method takes the name of the app (you can choose any name) and a second parameter [&#8216;ngRoute&#8217;]<\/p>\n<p>The second parameter ngRoute according to <a href=\"https:\/\/www.w3schools.com\/angular\/angular_routing.asp\" target=\"_blank\" rel=\"nofollow noopener\">W3school <\/a>&#8216;allows you to navigate to different pages in your application, while the application remains a single page application(SPA) with no page reloading.<\/p>\n<p>&nbsp;<\/p>\n<p><b>The GetList() Function<\/b><br \/>\nThis function could have any name.<br \/>\nIt is the function that contains the code that actually performs the query to fetch data from the SharePoint list.<\/p>\n<p>I have added line numbers to the code below, for easy explanation<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"line-height: 125%; margin: 0;\">     <span style=\"color: #008800; font-weight: bold;\">function<\/span> GetList($scope, listName){    \r\n            <span style=\"color: #008800; font-weight: bold;\">var<\/span> requestUrl <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"sites\/KindsonTheGenuis\/Lessons\/2018\/_api\/web\/lists\/getbytitle('\"<\/span> <span style=\"color: #333333;\">+<\/span> listName <span style=\"color: #333333;\">+<\/span> <span style=\"background-color: #fff0f0;\">\"')\/items\"<\/span>;           \r\n            $.ajax({    \r\n            url<span style=\"color: #333333;\">:<\/span> requestUrl,    \r\n            method<span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"GET\"<\/span>,    \r\n            async<span style=\"color: #333333;\">:<\/span> <span style=\"color: #008800; font-weight: bold;\">false<\/span>,    \r\n            headers<span style=\"color: #333333;\">:<\/span> { <span style=\"background-color: #fff0f0;\">\"Accept\"<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"application\/json;odata=verbose\"<\/span> },    \r\n            success<span style=\"color: #333333;\">:<\/span> <span style=\"color: #008800; font-weight: bold;\">function<\/span>(data){    \r\n                $scope.items <span style=\"color: #333333;\">=<\/span> data.d.results;    \r\n            },    \r\n            error<span style=\"color: #333333;\">:<\/span> <span style=\"color: #008800; font-weight: bold;\">function<\/span>(sender,args){    \r\n                console.log(args.get_message());    \r\n            }    \r\n        });    \r\n  }<\/pre>\n<p><b>Listing 1.2<\/b>: AngularJS code to make GET Request to a URL<\/p>\n<p>&nbsp;<\/p>\n<p>The function GetList in line 1 takes two parameters. The first parameter is $scope. This parameters makes is possible for\u00a0 objects declared in the controller code to be available for use in the html page.<br \/>\nThe second parameter listName is the name of the SharePoint list you want to access.<\/p>\n<p>&nbsp;<\/p>\n<p><b>The $.ajax() Method<\/b><br \/>\nThe $.ajax() method is used to\u00a0 create ajax request.<br \/>\nIt takes the following parameter:<br \/>\n<i>url<\/i>: a string value of the request url to send the request to<br \/>\n<i>async<\/i>: indicates if whether the request should be<br \/>\n<i>method<\/i>: specified the HTML method to use for the request. Here we use a GET methods since we are only retrieving data<br \/>\n<i>headers<\/i>: specifies the headers\u00a0 such as content-type<\/p>\n<p>The next two parameters are functions discussed next<\/p>\n<p>&nbsp;<\/p>\n<p><b>Success Function\u00a0<\/b><br \/>\nThis is a function that is run if the request is successfully\u00a0 executed. In this case, the function takes the results of the request and assigns it to the items collection of the $scope object. This makes is possible that the results are available in the view(html page)<\/p>\n<p>&nbsp;<\/p>\n<p><b>Error Function<\/b><br \/>\nThis is the function that is run if the request fails. In this case a message containing the error is written to the console.<\/p>\n<p>&nbsp;<\/p>\n<h3 id=\"t4\">4. Understanding the Pieces of the your html page<\/h3>\n<p>The page displays the data from the request. So how does this work?<br \/>\nRemember that the data from the request was put in $scope.items. This means that it\u00a0 can be accessed from the html page.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #007700;\">&lt;div<\/span> <span style=\"color: #0000cc;\">ng-app=<\/span><span style=\"background-color: #fff0f0;\">\"listApp\"<\/span><span style=\"color: #007700;\">&gt;<\/span>    \r\n    <span style=\"color: #007700;\">&lt;div<\/span> <span style=\"color: #0000cc;\">id=<\/span><span style=\"background-color: #fff0f0;\">\"App1\"<\/span> <span style=\"color: #0000cc;\">ng-controller=<\/span><span style=\"background-color: #fff0f0;\">\"controller1\"<\/span><span style=\"color: #007700;\">&gt;<\/span>    \r\n        <span style=\"color: #007700;\">&lt;h1&gt;<\/span>First List Items<span style=\"color: #007700;\">&lt;\/h1&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;table<\/span> <span style=\"color: #0000cc;\">border=<\/span><span style=\"background-color: #fff0f0;\">\"1\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;tr&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;th&gt;<\/span>Firstname<span style=\"color: #007700;\">&lt;\/th&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;th&gt;<\/span>Lastname<span style=\"color: #007700;\">&lt;\/th&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/tr&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;tr&gt;<\/span>\r\n\r\n<span style=\"color: #007700;\">&lt;\/tr&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;tr<\/span> <span style=\"color: #0000cc;\">ng-repeat=<\/span><span style=\"background-color: #fff0f0;\">\"item in items\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;td&gt;<\/span>{{item.Firstname}}<span style=\"color: #007700;\">&lt;\/td&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;td&gt;<\/span>{{item.Lastname}}<span style=\"color: #007700;\">&lt;\/td&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/tr&gt;<\/span>\r\n\r\n<span style=\"color: #007700;\">&lt;\/table&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/div&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/div&gt;<\/span>\r\n<\/pre>\n<p><b>Listing 1.3:<\/b> HTML Page displaying AngularJS Content<\/p>\n<p>&nbsp;<\/p>\n<p><b>The ng-app directive<\/b><br \/>\nThis helps you to specify the root element of the AngularJs application. All AngularJS application must have a root element in the html page.<br \/>\nIf this code is specified in a html element, then AngularJS code could be written inside this element. In our case we made the table element the root element. But you can also place this code in the body element of the html code.<\/p>\n<p>&lt;table ng-app=&#8217;modulename&#8217;&gt;<br \/>\n&#8230;.<br \/>\nAngularJS code can go here&#8230;<br \/>\n&#8230;..<br \/>\n&lt;\/table<br \/>\nmodulename specifies the name of the module to load with the application. Remember that the modulename is given as parameter to the angular.module() method.<\/p>\n<p><i>Quiz: What is the modulename from our code? \u263a<\/i><\/p>\n<p>&nbsp;<\/p>\n<p><b>The ng-controller directive<\/b><br \/>\nThe ng-controller directive attaches a controller to the view(html page).<br \/>\nThis means that the functions and variables defined in the controller will be available in the view.(remember, our controller is Controller1)<\/p>\n<p>&nbsp;<\/p>\n<p><b>The ngRepeat directive<\/b><br \/>\nThe ngRepeat directive is added to the html page to iterate through a collection of items. Look at the code fragment so you could understand how it works.<\/p>\n<p>In our case we have the code below<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #007700;\">&lt;tr<\/span> <span style=\"color: #0000cc;\">ng-repeat=<\/span><span style=\"background-color: #fff0f0;\">\"item in items\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;td&gt;<\/span>{{item.Firstname}} <span style=\"color: #007700;\">&lt;\/td&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;td&gt;<\/span>{{item.Lastname}} <span style=\"color: #007700;\">&lt;\/td&gt;<\/span>\r\n\r\n<\/pre>\n<p><b>Listing 1.4<\/b>: Using ng-repeat to display a table<\/p>\n<p>&nbsp;<\/p>\n<p>This code would create as many rows as there are items in the $scope<\/p>\n<p>&nbsp;<\/p>\n<p><b>Specifying Repeat Items<\/b><br \/>\nRepeat items are specified one element below the element containing the ngRepeat directive. So in our case, the ngRepeat directive is in &lt;tr&gt; element, therefore, the repeat elements would be in the &lt;td&gt; tag.<br \/>\nAlso, the repeat variables are enclosed in double curly braces, like this {{}}<\/p>\n<p><b><br \/>\n<\/b><b><\/b><\/p>\n<h3 id=\"t5\">5. Final Notes<\/h3>\n<p>If you have followed this tutorial up to the end, thumbs up to you. If you have any challenges or observation, you can let me know in the comment box below or in the form to the left of this page.<\/p>\n<p>I would also like to let you know that the key to catching up on software tutorials is to practice them yourself. Secondly, you can get assistance from experienced professionals like me!<br \/>\nFeel free to leave me a message or question<\/p>\n<p>Thanks for your efforts!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am Kindson and today, I&#8217;m going to explain to you how to fetch Sharepoint using AngularJS list and display it in html page. I &hellip; <\/p>\n","protected":false},"author":2,"featured_media":643,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[113],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/82"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=82"}],"version-history":[{"count":6,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/82\/revisions"}],"predecessor-version":[{"id":1218,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/82\/revisions\/1218"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media\/643"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}