In Part 1 , and Part 2 of the series, we walked through setting up of project and App.js respectively. In this concluding post, we will look at how the html files are coded.
Tag: AngularJS
Building Single Page Applications (SPA) with AngularJS and SharePoint 2013 – Part II
In Part 1 of the series, we walked through the initial steps of setting up the project and adding all related files. Now, let us focus on the App.js file (remember this is just for demo purpose, you would never want to put all your controller code in one single file, separate them based on the controllers)
Building Single Page Applications (SPA) with AngularJS and SharePoint 2013 – Part I
This is Part 1 of Building Single Page Applications (SPA) with AngularJS ad SharePoint 2013. We will stick with the main moving parts of building SPA with SP 2013 and AngularJS. Assumption here is that you have played a little with AngularJS before.
<img src=”../images/ajax_loader.gif” alt=”Loading…” title=”Loading..” style=”height: 50px;” />
Consume SharePoint 2013 REST API using AngularJS
The code below shows you how to invoke a SharePoint 2013 REST API. Understand this code is listed just for you to understand how to invoke REST API. In the real world, you would want to separate the script file into its own .js file. Recommended practice is to have an individual file for each controller. Also, http service is asynchronous here, so should always have on success and on failure events.
I have a list called Customers in SP 2013, so the REST API call would look something like this:
http://webapp/_api/web/lists/getByTitle(‘Customers’)/items?$select=CustomerID1,CustomerName,CustomerAddress,CustomerState,CustomerCountry
Also, to note is that I am getting back the REST API output as JSON, with the following statement: application/json; odata=verbose
If you examine JSON output from SP, the informatio you need resides within the details structure, so that is the reason you see that I am storing my customer data by accessing <dataoutputfromJSON>.d.details
Here is the complete HTML file to access SharePoint 2013 list via REST API using AngularJS
<html ng-app=”tkCustomerApp”>
<head>
<title>SharePoint REST API Call </title>
<script src=”scripts/angular.js”></script>
<script>
var tkCustomerApp= angular.module(‘tkCustomerApp’, []);
tkCustomerApp.controller(‘CustomerCtrl’, function ($scope, $http){
$http({headers: { “Accept”: “application/json; odata=verbose” }, method: ‘GET’, url:”http://tksp15:90/_api/web/lists/getByTitle(‘Customers’)/items?$select=CustomerID1,CustomerName,CustomerAddress,CustomerState,CustomerCountry”})
.success(function(data) {
$scope.customers = data.d.results;
});
});
</script>
</head>
<body ng-controller=”CustomerCtrl”>
<div> Search:<input ng-model=”query” type=”text”/> </div>
<br />
<table>
<tr>
<th>Customer ID</th>
<th>Customer Name</th>
<th>Customer Address</th>
<th>Customer State</th>
<th>Customer Country</th>
</tr>
<tr ng-repeat=”cust in customers | filter:query”>
<td>{{cust.CustomerID1}}</td>
<td>{{cust.CustomerName}}</td>
<td>{{cust.CustomerAddress}}</td>
<td>{{cust.CustomerState}}</td>
<td>{{cust.CustomerCountry}}</td>
</tr>
</table>
</body>
</html>