SharePoint Pals
 | Sign In
How to Create Custom Service in AngularJS for CRUD Operations on SharePoint List
by Ahamed Fazil Buhari 11 Oct 2016
SharePoint Developer
Today  :   5     Total  :    562

In this article, we’ll look into the advantages and how to implement Custom Services in Angular. Before going further, please refer my previous articles on $http service in Angular. Here, I’ve implemented CRUD operations on SharePoint List using Angular HTTP service and having that in my own Custom service.

‘Why would we need to write a custom service?’ the answer is simple, Services are useful in various scenarios,

· Packaging Reusable Logic – we can have some logic (an algorithm or some piece of code that we need in several different controllers) in a container and we can use that in different places, it would avoid duplicating of code.

· Shared Data - If we want to share data between the controllers on different views, a service will be a great place to store data. Because Angular only instantiate a single instance of each service in an application.

· Complexity Is Reduced – Sometimes a controller would be too difficult to manage if we have so much code in it. So in that situation we can simplify the code and separate the responsibilities and give some burden to the service.

· Clean Maintainable Code – Organizing your code into services leads to cleaner, better defined components which means we have an application with more maintainable code.

In the following example, we will have SharePoint List Item CRUD operation in an Angular Custom Service named as, MyCustomService.js

 (function () { //IIFE 
     //RequestHeader for get,post,update,delete
     var requestHeader = {
         getHeader: {
             'headers': {
                 'accept': 'application/json;odata=verbose'
         postHeader: {
             'headers': {
                 "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                 'content-type': 'application/json;odata=verbose',
                 'accept': 'application/json;odata=verbose'
         deleteHeader: {
             'headers': {
                 "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                 'content-type': 'application/json;odata=verbose',
                 "IF-MATCH": "*"
         updateHeader: {
             'headers': {
                 "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                 'content-type': 'application/json;odata=verbose',
                 'accept': 'application/json;odata=verbose',
                 "IF-MATCH": "*",
                 "X-HTTP-Method": "MERGE"
     //A service can have dependency on another service – Here we’ve used $http service
     var CRUD_SP = function ($http) {
 	 //Defining Function with parameter, 
         //same number of parameter we use when we invoke this function	
         var getListData = function (urlValue) {
             return $http.get(urlValue, requestHeader.getHeader)
 						.then(function (response) {
         var setListData = function (urlValue, data) {
             return $, data, requestHeader.postHeader)
 						.then(function (response) {
 						    return 'Created from Custom Service...';
         var deleteListData = function (urlValue) {
             return $http.delete(urlValue, requestHeader.deleteHeader)
 						.then(function (response) {
 						    return 'Deleted from Custom Service...';
         var updateListData = function (urlValue, data) {
             return $, data, requestHeader.updateHeader)
 						.then(function (response) {
 						    return 'Updated from Custom Service...';
         //When someone invokes this CRUD_SP function
         //then it'll return an Object that is of CRUD_SP
         return {
             //Pick up the Public API
             //name: function_name to call
             getListData: getListData,
             setListData: setListData,
             updateListData: updateListData,
             deleteListData: deleteListData
     //Creating the reference to "myapp" which we defined in MyAngular.js and 
     //No 2nd parameter, bcz I'm not trying to create a Module
     //rest of the code goes above this module.
     var module = angular.module("myapp");
     //Registering our service, so that Angular will use this
     //There are various ways to Register the service
     //This is simple and straightforward way,
     //Make use of method called 'factory('Name of the service', function name that returs an object)'
     module.factory('CRUD_SP', CRUD_SP);

The above js file will be used as a Custom Service. The code written in our main JS file has been reduced and we can use this custom service in other places in our application. The main JS file code has been given below,

 (function () { //IIFE 
     //angular.module(,) in this second parameter is an array, 
     //earlier we had empty bcz we don't have any explicit dependencies,
     var app = angular.module("myapp", []);
     //Add our new service in the controller parameter
     var MyController = function ($scope, CRUD_SP) {
         var urlVal = {
             url: _spPageContextInfo.webAbsoluteUrl + "/_api" + "/web/lists/GetByTitle('Phone')/Items"
         //Function to call for Successful GET
         var onSuccessGet = function (data) {
             $scope.people = data;
         //Function to call for Successful post,update,delete
         var onSuccess = function (data) {
             $scope.phonename = '';
             $scope.pplusing = '';
             $scope.newphonename = '';
             $scope.newpplusing = '';
             $scope.itemID = '';
             $scope.error = '';
         var onError = function (reason) {
             $scope.error = "something went wrong";
         $scope.getItem = function () {
             var geturlVal = urlVal.url + "/?$select=*";
             //using CRUD_SP custom service and calling function defined inside it
 				   .then(onSuccessGet, onError);
         $scope.saveItem = function () {
             var createURL = urlVal.url;
             var data = {
                 //To Get __metadata->'type' value for the list, go to
                 //https://<site>/_api/web/lists/getbytitle('<List Name>')?$select=ListItemEntityTypeFullName
                 //from the xml, get the value inside <d:ListItemEntityTypeFullName> element
                 __metadata: { 'type': 'SP.Data.TestingListItem' },
                 Title: $scope.phonename,
                 People_x0020_using: $scope.pplusing
             CRUD_SP.setListData(createURL, data)
                    .then(onSuccess, onError);
         $scope.deleteItem = function () {
             var deleteURL = urlVal.url + '(' + $scope.itemID + ')';
 	    		.then(onSuccess, onError);
         $scope.updateItem = function () {
             var updateURL = urlVal.url + '(' + $scope.itemID + ')';
             var updatedata = {
                 __metadata: { 'type': 'SP.Data.TestingListItem' },
                 Title: $scope.newphonename,
                 People_x0020_using: $scope.newpplusing
             CRUD_SP.updateListData(updateURL, updatedata)
 	    		.then(onSuccess, onError);
     app.controller("MyController", MyController);

In the aspx page, we’re having buttons and text fields to update, delete and insert list items and HTML table to show the data.

 <script src="../SiteAssets/JS/Form JS/jquery-1.10.2.js"></script>
 <script src=""></script>
 <script src="../SiteAssets/JS/Form JS/MyAngular.js"></script>
 <!-- Make sure your service is defined after Main JS i.e MyAngular.js,
 bcz module has been defind in MyAngular.js!-->
 <script src="../SiteAssets/JS/Form JS/MyCustomService.js"></script>
 <div id="appDiv" ng-app="myapp">
     <div id="controllerDiv" ng-controller="MyController">
         <label ng-show="error" class="ng-hide">Error : {{error}}</label>
         <h2>Insert into List</h2>
         Phone Name : <input type="text" ng-model="phonename" />
         No. of People Usng : <input type="number" ng-model="pplusing" />
         <button type="button" ng-click="saveItem()">Save</button>
         <h2>Delete or Update List Item</h2>
         Enter Item ID : <input type="text" ng-model="itemID" />
         New Phone Name : <input type="text" ng-model="newphonename" />
         New No. of People Usng : <input type="number" ng-model="newpplusing" />
         <button type="button" ng-click="deleteItem()">Delete</button>
         <button type="button" ng-click="updateItem()">Update</button>
         <button type="button" ng-click="getItem()">Get Items</button>
         <div ng-show="people" class="ng-hide">
             <table id="userDetails" border="1px solid">
                         <th>Item ID</th>
                         <th>Phone Title</th>
                         <th>No. of ppl using</th>
                     <tr ng-repeat="ppl in people">
                         <td>{{ppl.People_x0020_using | number}}</td>


Happy Coding


blog comments powered by Disqus

SharePoint Pals

SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile


Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community

SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to in a word document.

Related Resources

Recent Tweets

Twitter October 23, 22:21
How to Add/Remove User Custom Actions (in Site Actions Menu) Programmatically using CSOM PNP in SharePoint -

Twitter October 21, 21:34
How to Add a JS Link Reference to the Display Form or Any other ASPX Programmatically using CSOM PNP in SharePoint -

Twitter October 20, 13:01
How to Add a JS Link Reference to the NewForm and EditForm Programmatically using CSOM PNP in SharePoint Office 365-

Twitter October 12, 12:15
How to Deploy Provider Hosted Apps (Add-Ins) by App Stapling in SharePoint Office 365 -

Twitter October 11, 13:39
How to Deploy Provider HostedApp programmatically using CSOM in SharePoint Office 365 Activating Developer Feature -

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation