Tuesday, October 7, 2014

SharePoint 2013: List operation using AngularJS Part 6

This post will explain you about how to add/Delete List. I have added some validation rules which may help you setup in HTML page using "required" tag of AngularJS

Service:
    this.AddList = function (listName) {
        var item =JSON.stringify({
            "__metadata": { "type": "SP.List" },
            "AllowContentTypes": "true",
            "BaseTemplate": "104",
            "ContentTypesEnabled": "true",
            "Description": "List created by AngularJS service call",
            "Title": listName
        });
        var config = {
            headers: {
                "Content-Type": "application/json;odata=verbose",
                "ACCEPT": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            }
        };
        return $http.post(siteUrl + "/_api/web/lists", item, config);
    };
    this.GetListByName = function (listName) {
        var config = {
            headers: {
                "ACCEPT": "application/json;odata=verbose"
            }
        };
        return $http.get(siteUrl + "/_api/web/lists?$filter=Title eq '" + listName+"'", config);
    };

    this.DeleteList = function (data) {
        var config = {
            contentType: "application/json;odata=verbose",
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "X-HTTP-Method": "DELETE",
                "If-Match": data.__metadata.etag
            }
        };
        return $http.post(data.__metadata.uri, null, config);
    };


Controller:
$scope.AddList = function () {
        ListService.AddList($scope.listName)
        .success(function () {
            alert("list created");
            $scope.$broadcast("RefillList");
        })
        .error(function () {
            alert("fail to add list");
        });
    };
    $scope.DeleteList = function () {
        ListService.GetListByName($scope.listName)
        .success(function (data) {
            var result = data.d.results[0];
            ListService.DeleteList(result)
            .success(function () {
                alert("list deleted");
                $scope.$broadcast("RefillList");

            })
            .error(function () {
                alert("failed to delete list");
            });
        })
        .error(function () {
            alert("error");
        });
    };
    $scope.$on('RefillList', function () {
        ListService.GetLists()
        .success(function (data) {
            var results = data["d"]["results"];
            $scope.lists = results;
        })
        .error(function () {
            alert("error while getting items");
        });
    });


HTML:
<ng-form name="listForm">
<div> Total List</div>
    <input ng-model="listName" name="listName" required placeholder="List Name" />
    <span class="error" ng-show="listForm.listName.$error.required">Required!</span>
    <input type="button" ng-click="AddList()" value="Add List" ng-disabled="!listForm.$valid" /> 
    <input type="button" ng-click="GetLists()" value="GetLists" />
    <input type="button" ng-click="DeleteList()" value="DeleteList" ng-disabled="!listForm.$valid"/>

<ul>
    <li ng-repeat="list in lists">
        <a ng-click="GetListItems(list.Title)">{{list.Title}}</a>
        </li>
    </ul>

    <ul>
    <li ng-repeat="listItem in listItems">
        <a ng-click="GetListItemsById(listItem.Id,listItem.Title)">{{listItem.Title}}</a>
    </li>
</ul>
    </ng-form>
<ng-form name="itemForm">
<input ng-model="listName" name="listName" placeholder="List Name" required />
    <span class="error" ng-show="itemForm.itemForm.$error.required"></span>
    <input ng-model="itemId" name="itemId" ng-readonly="true" placeholder="Id" />
    <input ng-model="iTitle" placeholder="Title" required name="iTitle"  />
    <input type="button" ng-click="AddListItem()" value="AddListItem" ng-disabled="!itemForm.$valid" />
    <input type="button" ng-click="UpdateListItem()" value="UpdateListItem" ng-disabled="!itemForm.$valid" />
    <input type="button" ng-click="DeleteListItem()" value="DeleteListItem" ng-disabled="!itemForm.$valid" />
</ng-form>
<div ng-hide="true">
    {{jsonData}}
</div>
<div ng-hide="true">
    {{listItemJSON}}
</div>
    

Monday, October 6, 2014

SharePoint 2013: List operation using AngularJS Part 5

This post will explain you about how to delete list item. 
Add Below function to Service file

 this.DeleteListItem = function (data) {
        var config = {
            contentType: "application/json;odata=verbose",
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "X-HTTP-Method": "DELETE",
                "If-Match": data.__metadata.etag
            }
        };
        return $http.post(data.__metadata.uri, null , config);
    };

Add Below code to Controller file.

 $scope.DeleteListItem = function () {
        ListService.GetListItemsById($scope.listName, $scope.itemId)
        .success(function (data) {
            var result = data.d.results[0];
            ListService.DeleteListItem(result)
            .success(function () {
                alert("item deleted");
                $scope.$broadcast("RefillData");
            })
            .error(function () {
                alert("delete failed");
            });
        })
        .error(function () {
            alert("error");
        });
    };

Update below code from HTML file

List Name : <input ng-model="listName" />ID: <input ng-model="itemId" ng-readonly="true" /> Title : <input ng-model="iTitle" /><input type="button" ng-click="AddListItem()" value="AddListItem" /><input type="button" ng-click="UpdateListItem()" value="UpdateListItem" /><input type="button" ng-click="DeleteListItem()" value="DeleteListItem" />


SharePoint 2013: List operation using AngularJS Part 4

There are some changes in Add/Retrieve list item methods from previous posts. Please make a note about that. This post will explain you about how to update list item.

HTML:

<div> Total List</div>
<input type="button" ng-click="GetLists()" value="GetLists" />
<ul>
    <li ng-repeat="list in lists">
        <a ng-click="GetListItems(list.Title)">{{list.Title}}</a>
        </li>
    </ul>

    <ul>
    <li ng-repeat="listItem in listItems">
        <a ng-click="GetListItemsById(listItem.Id,listItem.Title)">{{listItem.Title}}</a>
    </li>
</ul>
List Name : <input ng-model="listName" />ID: <input ng-model="itemId" ng-readonly="true" /> Title : <input ng-model="iTitle" /><input type="button" ng-click="AddListItem()" value="AddListItem" /><input type="button" ng-click="UpdateListItem()" value="UpdateListItem" />
<div ng-hide="true">
    {{jsonData}}
</div>
<div ng-hide="true">
    {{listItemJSON}}
</div>

Controller: 

var listModule = angular.module('listModule', []);
listModule.controller('listController', ['$scope', 'ListService', function ($scope, ListService) {
    $scope.GetLists = function () {
        ListService.GetLists()
        .success(function (data) {
            $scope.jsonData = JSON.stringify(data);
            var results = data["d"]["results"];
            $scope.lists = results;
        })
        .error(function () {
            alert("error");
        });
    };
    $scope.GetListItems = function (listName) {
        $scope.listName = listName;
        ListService.GetListItems(listName)
        .success(function (data) {
            $scope.listItemJSON = JSON.stringify(data);
            var results = data["d"]["results"];
            $scope.listItems = results;
        })
        .error(function () {
            alert("error");
        });
    };
    $scope.GetListItemsById = function (listId,listTitle) {
        
        $scope.itemId = listId;
        $scope.iTitle = listTitle;
    };

    $scope.AddListItem = function () {
        ListService.AddItem($scope.listName, $scope.iTitle)
        .success(function () {
            $scope.$broadcast('RefillData');
         })
        .error(function () {
            alert("fail");
        });
    };
    $scope.UpdateListItem = function () {
        ListService.GetListItemsById($scope.listName, $scope.itemId)
        .success(function (data) {
            var result = data.d.results[0];
            ListService.UpdateListItem(result, $scope.listName, $scope.iTitle)
            .success(function () {
                $scope.$broadcast('RefillData');
            })
            .error(function () {
                alert("update failed");
            });
        })
        .error(function () {
            alert("error");
        });
    };
    $scope.$on('RefillData', function () {
        ListService.GetListItems($scope.listName)
        .success(function (data) {
            var results = data["d"]["results"];
            $scope.listItems = results;
        })
        .error(function () {
            alert("error while getting items");
        });
    });
}]);

Service: 

var listServiceModule = angular.module('listServiceModule', []);
listServiceModule.service('ListService', ['$http', function ($http) {
    var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
    this.GetLists = function () {
        var config = {
                headers: {
                    "ACCEPT": "application/json;odata=verbose"
                }
        };
        return $http.get(siteUrl + "/_api/web/lists", config);
    };

    this.GetListItems = function (listName) {
        var config = {
            headers: {
                "Accept": "application/json;odata=verbose"
            }
        };
        return $http.get(siteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items", config);
    };

    this.GetListItemsById = function (listName,itemId) {
        var config = {
            headers: {
                "Accept": "application/json;odata=verbose"
            }
        };
        return $http.get(siteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items?$filter=Id eq " + itemId, config);
    };

    
    this.AddItem = function (listName, iTitle) {
        var item = JSON.stringify({ '__metadata': { 'type': 'SP.List' }, 'Title': iTitle });
        var config = {
            headers: {
                "Content-Type": "application/json;odata=verbose",
                "ACCEPT": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            }
        };
        return $http.post(siteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items", item, config);
    };

    this.UpdateListItem = function (data, listName, iTitle) {
        var item = JSON.stringify({ '__metadata': { 'type': 'SP.List' }, 'Title': iTitle });
        var config = {
            contentType: "application/json;odata=verbose",
            headers: {
                "Accept": "application/json;odata=verbose",
                "Content-Type": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "X-HTTP-Method": "MERGE",
                "If-Match": data.__metadata.etag
            }
        };
        return $http.post(data.__metadata.uri, item, config);
    };
}]);

Friday, October 3, 2014

SharePoint 2013: List operation using AngularJS Part 3

This post will explain you about how to retrieve List/ List Items and add List Item.

There is one more important point needs to understand in AngularJS boradcast/emit/on event. Here we have only one controller but this will help you to broadcast or emit event from parent to child or child to parent. Its just like a listener.

HTML:

<div> Total List</div>
<input type="button" ng-click="GetLists()" value="GetLists" />
<ul>
    <li ng-repeat="list in lists">
        <a ng-click="GetListItemsByName(list.Title)">{{list.Title}}</a>
        </li>
    </ul>

    <ul>
    <li ng-repeat="listItem in listItems">
        {{listItem.Title}}
    </li>
</ul>


List Name : <input ng-model="listName" /> Title : <input ng-model="iTitle" /><input type="button" ng-click="AddListItem()" value="AddListItem" />


<div>
    {{jsonData}}
</div>
<div>
    {{listItemJSON}}
</div>

Controller:

var listModule = angular.module('listModule', []);
listModule.controller('listController', ['$scope', 'ListService', function ($scope, ListService) {
    $scope.GetLists = function () {
        ListService.GetLists()
        .success(function (data) {
            $scope.jsonData = JSON.stringify(data);
            var results = data["d"]["results"];
            $scope.lists = results;
        })
        .error(function () {
            alert("error");
        });
    };
    $scope.GetListItems = function () {
        ListService.GetListItems($scope.listName)
        .success(function (data) {
            $scope.listItemJSON = JSON.stringify(data);
            var results = data["d"]["results"];
            $scope.listItems = results;
        })
        .error(function () {
            alert("error");
        });
    };
    $scope.GetListItemsByName = function (listName) {
        $scope.listName = listName;
        ListService.GetListItems(listName)
        .success(function (data) {
            $scope.listItemJSON = JSON.stringify(data);
            var results = data["d"]["results"];
            $scope.listItems = results;
        })
        .error(function () {
            alert("error");
        });
    };
    $scope.AddListItem = function () {
        ListService.AddItem($scope.listName, $scope.iTitle)
        .success(function () {
            $scope.$broadcast('RefillData');
         })
        .error(function () {
            alert("fail");
        });
    };
    $scope.$on('RefillData', function () {
        ListService.GetListItems($scope.listName)
        .success(function (data) {
            var results = data["d"]["results"];
            $scope.listItems = results;
        })
        .error(function () {
            alert("error while getting items");
        });
    });
}]);

Service:

var listServiceModule = angular.module('listServiceModule', []);
listServiceModule.service('ListService', ['$http', function ($http) {
    var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
    this.GetLists = function () {
        var config = {
                headers: {
                    "ACCEPT": "application/json;odata=verbose"
                }
        };
        return $http.get(siteUrl + "/_api/web/lists", config);
    };

    this.GetListItems = function (listName) {
        var config = {
            headers: {
                "Accept": "application/json;odata=verbose"
            }
        };
        return $http.get(siteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items", config);
    };
    
    this.AddItem = function (listName, iTitle) {
        var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
        var itemType = GetItemTypeForListName(listName);
        var item = {
            "__metadata": { "type": itemType },
            "Title": iTitle
        };
        var config = {
            headers: {
                "Content-Type": "application/json;odata=verbose",
                "ACCEPT": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            }
        };
        return $http.post(siteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items", item, config);
    };
}]);

Functions:

function GetItemTypeForListName(name) {
    return "SP.Data." + name.charAt(0).toUpperCase() + name.slice(1) + "ListItem";
}


Thursday, October 2, 2014

SharePoint 2013: List operation using AngularJS Part 2

Below example explains you about how to get List Items using AngularJS service.

HTML Code:
<input ng-model="listName" /> : <input type="button" ng-click="GetListItems()" value="GetListItems" />
<ul>
    <li ng-repeat="listItem in listItems">
        {{listItem.Title}}
    </li>
</ul>
<div>
    {{listItemJSON}}
</div>

Controller File:
var listModule = angular.module('listModule', []);
listModule.controller('listController', ['$scope', 'ListService', function ($scope, ListService) {
    $scope.GetLists = function () {
        ListService.GetLists()
        .success(function (data) {
            $scope.jsonData = JSON.stringify(data);
            var results = data["d"]["results"];
            $scope.lists = results;
        })
        .error(function () {
            alert("error");
        });
    };
    $scope.GetListItems = function () {
        ListService.GetListItems($scope.listName)
        .success(function (data) {
            $scope.listItemJSON = JSON.stringify(data);
            var results = data["d"]["results"];
            $scope.listItems = results;
        })
        .error(function () {
            alert("error");
        });
    };
}]);


Service File:
var listServiceModule = angular.module('listServiceModule', []);
listServiceModule.service('ListService', ['$http', function ($http) {
    var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
    this.GetLists = function () {
        
        var config = {
                headers: {
                    "ACCEPT": "application/json;odata=verbose"
                }
        };
        return $http.get(siteUrl + "/_api/web/lists", config);
    };

    this.GetListItems = function (listName) {
        var config = {
            headers: {
                "Accept": "application/json;odata=verbose"
            }
        };
        return $http.get(siteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items", config);
    };
  
}]);

SharePoint 2013: List operation using AngularJS Part 1

If you want to create structure for AngularJS  based project, Please follow my previous post to create it. Here I am mentioning module vise information. I have developed Service in AngularJS to get all list from SharePoint site.


HTML Code :

<div> Total List</div>

<input type="button" ng-click="GetLists()" value="GetLists" />

<ul>

    <li ng-repeat="list in lists">

        {{list.Title}}

        </li>

    </ul>

<br />

<div>

    {{jsonData}}

</div>

Config.js

var configModule = angular.module('configModule', [

    'ngRoute',

    'listModule',

    'listServiceModule'

]);

Controller File :

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

listModule.controller('listController', ['$scope', 'ListService', function ($scope, ListService) {

    $scope.GetLists = function () {

        ListService.GetLists()

        .success(function (data) {

            $scope.jsonData = JSON.stringify(data);

            var results = data["d"]["results"];

            $scope.lists = results;

        })

        .error(function () {

            alert("error");

        });

    };

}]);

Service File :

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

listServiceModule.service('ListService', ['$http', function ($http) {

    var siteUrl = _spPageContextInfo.siteAbsoluteUrl;

    this.GetLists = function () {

       

        var config = {

                headers: {

                    "ACCEPT": "application/json;odata=verbose"

                }

        };

        return $http.get(siteUrl + "/_api/web/lists", config);

    };

}]);

Common Function File :

function GetItemTypeForListName(name) {

    return "SP.Data." + name.charAt(0).toUpperCase() + name.slice(1) + "ListItem";

}