Ram Prasad
 

Get all Site Users - SharePoint Server 2013 REST end point through Angular JS : Get the response in JSON format.

Apr, 22 2015
 
2 min/s
 
 

Intro

SharePoint Server 2013 provides many REST endpoints which can be used to consume data and perform most of the CRUD operations on a Site Collections. Consuming the REST end points through various JavaScript frameworks like Angular JS, Knockout JS etc., is the most widely used approach these days. These frameworks are built on the fundamentals of JSON and it's easy to integrate and develop scripts if we have our data in JSON format. But, the SharePoint Server 2013 REST APIs/end points return the response in ATOM or XML by default.

How to receive a JSON response from REST APIs?

We can instruct the server to give the response in a JSON format instead of ATOM or XML feed. When the REST end points are called, the Request Header is checked for the following property/attribute

Accept: application/json;odata=verbose

Once this property is found in the request header, the response is returned as JSON. So, this property should be added to the Request Header before the REST call is made. For Angular JS, refer the following script on how to add the header information. This script is a simple scenario which shows the code snippets to get the list of users in a site collection, by consuming the REST end points using Angular JS.

HTML Template

<html ng-app="AllSiteUsers"><head>
<title>All Site Users</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script src="script.js"></script>
</head>

<body ng-controller="GetAllUsers">

<table> <thead><th>Login Name</th><th>Display Name</th></thead> <tbody> <tr ng-repeat="user in users"> <td>{{user.LoginName}}</td> <td>{{user.Title}}</td> </tr> </tbody> </table> </body> </html>

script.js

var mod = angular.module('AllSiteUsers', []);

mod.controller('GetAllUsers', ['$scope', '$http', function ($scope, $http) {

$http.defaults.headers.common['Accept'] = 'application/json;odata=verbose';
var url='http://rams-sharepoint/sites/ram/_api/web/siteusers';
$http.get(url).
    success(function(data) {
        $scope.users = data.d.results;
    });

}]);