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

Posted: September 26, 2014 in SharePoint2013

Introduction

In this article we discuss about to get the data from SharePoint List using Angular js and Rest api. I used REST API to talk to SharePoint and get the data from the list. I am not going to discuss much about the REST services as many folks has already done a great work on explaining about REST API service.

In this script we just saw, 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 SharePoint list. $scope is a glue between 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 }}.

Picture3

<h1>WelCome To Angular JS Sharepoint 2013 REST API !!</h1>

<divng-app=”SharePointAngApp”class=”row”>

<divng-controller=”spCustomerController”class=”span10″>

<tableclass=”table table-condensed table-hover”>

<tr>

<th>Title</th>

<th>Employee</th>

<th>Company</th>

</tr>

<trng-repeat=”customer in customers”>

<td>{{customer.Title}}</td>

<td>{{customer.Employee}}</td>

<td>{{customer.Company}}</td>

</tr>

</table>

</div>

</div>

In the view you can use the ng-repeat to iterate through the customers and display customer information.
Use the following procedure to create a sample.

Step 1: Navigate to your SharePoint 2013 site.

Step 2: From this page select 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:

<style>

table, td, th {

border: 1pxsolidgreen;

}

th {

background-color: green;

color: white;

}

</style>

<scriptsrc=”https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js”></script&gt;

<scriptsrc=”http://code.jquery.com/ui/1.10.3/jquery-ui.min.js”></script&gt;

<script>

varmyAngApp = angular.module(‘SharePointAngApp’, []);

myAngApp.controller(‘spCustomerController’, function ($scope, $http) {

$http({

method: ‘GET’,

url: _spPageContextInfo.webAbsoluteUrl + “/_api/web/lists/getByTitle(‘InfoList’)/items?$select=Title,Employee,Company”,

headers: { “Accept”: “application/json;odata=verbose” }

}).success(function (data, status, headers, config) {

$scope.customers = data.d.results;

}).error(function (data, status, headers, config) {

});

});

</script>

<h1> Angular JS SharePoint 2013 REST API !!</h1>

<divng-app=”SharePointAngApp”class=”row”>

<divng-controller=”spCustomerController”class=”span10″>

<tableclass=”table table-condensed table-hover”>

<tr>

<th>Title</th>

<th>Employee</th>

<th>Company</th>

</tr>

<trng-repeat=”customer in customers”>

<td>{{customer.Title}}</td>

<td>{{customer.Employee}}</td>

<td>{{customer.Company}}</td>

</tr>

</table>

</div>

</div>

 Finally result show below:

Picture4

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s