Calling SharePoint 2013 REST api using an AngularJS Service

Calling the SharePoint 2013’s REST api can be a repetitive task. During the last Office365 summit in Amsterdam, the developer track always pointed to the Office365 Patterns and Practices for samples on how to develop in office 365. To my greatest joy they also have an SPA sample!

AngularJS services provide a way to share code within the app. This is great for not always having to write the same code over and over again. In this case calling SharePoint REST urls using $http.

The following is a snippet from

(function (angular) {
    "use strict";

        .module('sharepointproxy.service', [])
        .factory('SharepointProxyService', SharepointProxyService);

    function SharepointProxyService($http) {

        var proxyEndpointOptions = {
            url: 'api/sharepoint',
            method: 'POST',
            data: {}

        var defaultProxyOptions = {


        var service = {
            transformRequest: transformRequest,
            sendRequest: sendRequest,
            getWebTitle: getWebTitle

        function getData(o) {

        function transformRequest(request) {
            var transformedRequest = proxyEndpointOptions;
   = request;

            return transformedRequest;

        function sendRequest(request) {
            return $http(request)
                .then(function (data) {
                    return data;

        return service;

    SharepointProxyService.$inject = ['$http'];


It’s a great example on how to start using an AngularJS service calling SharePoint. It can be easily extended with more defined methods, like getting the title of the hostWeb:

        function getWebTitle(hostWebUrl) {
            var requestUrl = hostWebUrl + '/_api/web/title';
            var request = {
                url: requestUrl,
                method: 'GET',
                headers: {
                    Accept: 'application/json;odata=verbose'
            var spRequest = transformRequest(request);
            return sendRequest(spRequest);

All you need to add in your controller is a simple call to the service passing the hostWebUrl:

var queryParameterString = ([0] === '?') ? :;
$scope.queryParameters = deparam(queryParameterString);

        $scope.requestServiceData = function () {
            console.log("Request service data!");
            var result = SharepointProxyService.getWebTitle($scope.queryParameters.SPHostUrl)
            .then(function (data) {
                $scope.serviceResponse = data;

Finish off by adding a button to your HTML to call the service:

<button ng-click="requestServiceData()">Request</button>