SharePoint 2013: Filtering and Sorting of List Data using AngularJS and REST-API


This article explains how to filter and sort in SharePoint List data using Angular JS /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 article we just see that we have first created an Angular Controler 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 }}.we’ll be looking at a way to sort and filter our tabular data. This is a common feature that is always useful so let’s look at what we’ll be building and dive right into the code


We will implement on a Sample Application and try to get the data from the SharePoint list, bind the table and apply sort and filter to our tabular data.

Our application will allow us to:

  • Show a table of data (ng-repeat)
  • Sort by ascending or descending columns (orderBy)
  • Filter by using a search field (filter)

These are three common functions in any application and Angular lets us implement these features in a very simple way. Let’s set up our sample application’s HTML and Angular parts and then look at how we can sort and filter.


Use the following procedure to create a sample:

  1. <h1> Filtering and sorting in SharePoint List using AngularJS-REST-API !!</h1>  
  2. <br/><br/>  
  3. <divng-app="SharePointAngApp" class="row">  
  4.     <divng-controller="spCustomerController"  
  5.         class="span10">  
  6.         <div>  
  7.             Sort by:  
  8.             <selectng-model="sortExpression">  
  9.                 <optionvalue="Title">Title</option>  
  10.                     <optionvalue="Employee">Employee</option>  
  11.                         <optionvalue="Company">Company</option>  
  12.                             </select>  
  13.         </div>  
  15.         <br/> Search By Any:  
  16.         <inputtype="text" ng-model="search.$" />  
  17.         <br/>  
  18.         <br/>  
  19.         <tableclass="table table-condensed table-hover">  
  20.             <tr>  
  21.                 <th>Title</th>  
  22.                 <th>Employee</th>  
  23.                 <th>Company</th>  
  25.             </tr>  
  26.             <trng-repeat="customer in customers | orderBy:mySortFunction | filter:search">  
  27.                 <td>{{customer.Title}}</td>  
  28.                 <td>{{customer.Employee}}</td>  
  29.                 <td>{{customer.Company}}</td>  
  30.             </tr>  
  31.         </table>  
  32.     </div>  
  33. </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. th {  
  3.     background-color: #778899;  
  4.     color: white;  
  5. }  
  6. </style>  
  7. <scriptsrc="">  
  8.     </script>  
  9.     <scriptsrc="">  
  10.         </script>  
  12.         <script>  
  13. var myAngApp = angular.module(  
  14.     'SharePointAngApp', []);  
  15. myAngApp.controller(  
  16.     'spCustomerController',  
  17.     function($scope, $http)  
  18.     {  
  19.         $http(  
  20.         {  
  21.             method: 'GET',  
  22.             url: _spPageContextInfo.webAbsoluteUrl +  
  23.                 "/_api/web/lists/getByTitle('InfoList')/items?$select=Title,Employee,Company",  
  24.             headers:  
  25.             {  
  26.                 "Accept""application/json;odata=verbose"  
  27.             }  
  28.         }).success(function(data, status,  
  29.             headers, config)  
  30.         {  
  31.             $scope.customers = data.d.results;  
  32.             $scope.mySortFunction = function(  
  33.                 customer)  
  34.             { //Sorting Iteam  
  35.                 if (isNaN(customer[$scope.sortExpression]))  
  36.                     return customer[$scope.sortExpression];  
  37.                 return parseInt(customer[$scope  
  38.                     .sortExpression]);  
  39.             }  
  40.         }).error(function(data, status,  
  41.             headers, config) {  
  42.         });  
  43.     });  
  44.         </script>  
  46.         <h1> Filtering and sorting in SharePoint List using AngularJS-REST-API !!</h1>  
  47.         <br/><br/>  
  48.         <divng-app="SharePointAngApp" class="row">  
  49.             <divng-controller="spCustomerController"  
  50.                 class="span10">  
  51.                 <div>  
  52.                     Sort by:  
  53.                     <selectng-model="sortExpression">  
  54.                         <optionvalue="Title">Title</option>  
  55.                             <optionvalue="Employee">Employee</option>  
  56.                                 <optionvalue="Company">Company</option>  
  57.                                     </select>  
  58.                 </div>  
  60.                 <br/> Search By Any:  
  61.                 <inputtype="text" ng-model="search.$" />  
  62.                 <br/>  
  63.                 <br/>  
  64.                 <tableclass="table table-condensed table-hover">  
  65.                     <tr>  
  66.                         <th>Title</th>  
  67.                         <th>Employee</th>  
  68.                         <th>Company</th>  
  70.                     </tr>  
  71.                     <trng-repeat="customer in customers | orderBy:mySortFunction | filter:search">  
  72.                         <td>{{customer.Title}}</td>  
  73.                         <td>{{customer.Employee}}</td>  
  74.                         <td>{{customer.Company}}</td>  
  75.                 </tr>  
  76.             </table>  
  77.          </div>  
  78.     </div>  

Filtering the Table:

  1. <tr ng-repeat="customer in customers | filter:search">  
You will need to bind whatever input you are using with ng-model="search.$". Now as we type into that input box, you should see that table changes

Filtering the Table

Sorting the Table:
  1. <trng-repeat=”customer in customers | orderBy:mySortFunction”>  
Sorting the Table
That’s all we need to change the sort order of our ng-repeat. If you refresh your page, you’ll see that your list is sorted by Employee in normal order.


Similar Articles