Angular JS + Rest API + Getting List Data in SharePoint 2013

Introduction

This article explains how to get the data from a SharePoint List using Angular JavaScript and the REST API. I used the REST API to talk to SharePoint and get the data from the list. I am not going to discuss much about the REST services since many folks have already done great work on explaining REST API services.

In this script we just see that we have first created an Angular Controller with the name "spCustomerController". We have also injected $scope and $http service. The $http service will fetch the list data from the specific columns of the SharePoint list. $scope is a glue between a Controller and a View. It acts as execution context for Expressions. Angular expressions are code snippets that are usually placed in bindings such as {{ expression }}.

Angular Controller.jpg

Use the following procedure to create a sample.

  1. <h1>WelCome To Angular JS Sharepoint 2013 REST API !!</h1>  
  2.   
  3. <div ng-app="SharePointAngApp" class="row">  
  4.     <div ng-controller="spCustomerController" class="span10">  
  5.         <table class="table table-condensed table-hover">  
  6.             <tr>  
  7.                 <th>Title</th>  
  8.                 <th>Employee</th>  
  9.                 <th>Company</th>  
  10.                  
  11.             </tr>  
  12.             <tr ng-repeat="customer in customers">  
  13.                 <td>{{customer.Title}}</td>  
  14.                 <td>{{customer.Employee}}</td>  
  15.                 <td>{{customer.Company}}</td>  
  16.                 </tr>  
  17.         </table>  
  18.     </div>  
  19. </div>  
Step 1: Navigate to your SharePoint 2013 site.

Step 2: From this page select the Site Actions | Edit Page.

Edit the page, go to the "Insert" tab in the Ribbon and click the "Web Part" option. In the "Web Parts" picker area, go to the "Media and Content" category, select the "Script Editor" Web Part and press the "Add button".

Step 3: Once the Web Part is inserted into the page, you will see an "EDIT SNIPPET" link; click it. You can insert the HTML and/or JavaScript as in the following
  1. <style>  
  2. table, td, th {  
  3.     border: 1px solid green;  
  4. }  
  5.   
  6. th {  
  7.     background-color: green;  
  8.     color: white;  
  9. }  
  10. </style>  
  11. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>  
  12. <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js"></script>  
  13.   
  14. <script>  
  15.       
  16.   
  17.     var myAngApp = angular.module('SharePointAngApp', []);  
  18.     myAngApp.controller('spCustomerController', function ($scope, $http) {  
  19.         $http({  
  20.             method: 'GET',  
  21.             url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('InfoList')/items?$select=Title,Employee,Company",  
  22.             headers: { "Accept": "application/json;odata=verbose" }  
  23.         }).success(function (data, status, headers, config) {  
  24.             $scope.customers = data.d.results;  
  25.         }).error(function (data, status, headers, config) {  
  26.          
  27.         });  
  28.     });  
  29.       
  30.   
  31. </script>  
  32.   
  33. <h1> Angular JS SharePoint 2013 REST API !!</h1>  
  34.   
  35. <div ng-app="SharePointAngApp" class="row">  
  36.     <div ng-controller="spCustomerController" class="span10">  
  37.         <table class="table table-condensed table-hover">  
  38.             <tr>  
  39.                 <th>Title</th>  
  40.                 <th>Employee</th>  
  41.                 <th>Company</th>  
  42.                  
  43.             </tr>  
  44.             <tr ng-repeat="customer in customers">  
  45.                 <td>{{customer.Title}}</td>  
  46.                 <td>{{customer.Employee}}</td>  
  47.                 <td>{{customer.Company}}</td>  
  48.                 </tr>  
  49.         </table>  
  50.     </div>  
  51. </div>  
Finally the result show look as below:

 

Finally result show.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all