In this article, I will explain how to create SharePoint list items using Angular.js.This will help to create a new record for long SharePoint lists which contain 100+ fields so we no longer specify field names in this code.
Here we go,
- Create a custom SharePoint list (In this article, I named the list as SampleEmpList)
- Create columns in the list
- Create an HTML form based on the SharePoint columns type and link the script file which will help to create new records to SharePoint list.
Go to Add an app >> Custom List.
Name your list.
Create columns as shown below.
How it works -
I am taking one column to explain the procedure:
Step 1 - HTML code
Where DataBind is an object, Title is an internal name of list field.
So, we are passing the input value of the Title through the DataBind object.
Step2
Passing the object DataBind on the AddItem function as shown in the figure.
Onclick
Step3
AddItem function
Now, pass the DataBind object in this function as shown in below wIch returns data like
Title: Ram,
- Then, after filling all columns, the data will pass through the object.
- Convert the object data into JSON format as below.
var data = JSON.stringify(FormData);
- Then, call the HTTP call and set the data as below.
Code
- $scope.AddItem = function(FormData) {
- var data = JSON.stringify(FormData);
- data = data.replace(/[{}]/g, '');
- var datavalue = "{__metadata:{'type':'SP.Data." + listName + "ListItem'}," + data + "}";
- $http({
- url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('" + listName + "')/items",
- method: "POST",
- headers: {
- "Accept": "application/json;odata=verbose",
- "Content-Type": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "X-HTTP-Method": "POST"
- },
- data: datavalue
- }).then(function(response) {
- alert("success");
- }, function(response) {
- alert("failed");
- });
Finally, it will create a new record without mentioning the filed names separately.
HTML Code
- <html>
-
- <head> </head>
-
- <body ng-app="myApp" ng-controller="MainCtrl">
- <table class="table-condensed table-hover tbldata" style="width:100%">
- <thead>CreateForm</thead>
- <tbody>
- <tr>
- <td style="width:10%">Name</td>
- <td style="width:50%"><input class="form-control" ng-model="DataBind.Title" type="text" /></td>
- </tr>
- <tr>
- <td>Salary</td>
- <td style="width:50%"><input class="form-control" ng-model="DataBind.EmpSalary" type="number" /></td>
- </tr>
- <tr>
- <td>Designation</td>
- <td><input class="form-control" ng-model="DataBind.Designation" type="text" /></td>
- </tr>
- <tr>
- <td>Department</td>
- <td><select class="form-control" ng-model="DataBind.Department"><option value="">--Select an option--</option><option ng-repeat="Dept in Depts" value="{{Dept}}">{{Dept}}</option></select></td>
- </tr>
- <tr>
- <td>Address</td>
- <td><textarea class="form-control" ng-model="DataBind.EmpAddress" type="text"></textarea></td>
- </tr>
- <tr>
- <td>Gender</td>
- <td><label><input type="radio" ng-model="DataBind.Gender" value="Male"/>Male</label><label><input type="radio" ng-model="DataBind.Gender" value="Female"/>Female</label></td>
- </tr>
- <tr>
- <td>Employee Id</td>
- <td><input class="form-control" ng-model="DataBind.EmpId" type="number" /></td>
- </tr>
- <tr>
- <td>DOB</td>
- <td><input class="form-control" ng-model="DataBind.DOB" type="date" /></td>
- </tr>
- <tr>
- <td></td>
- <td><input type="button" ng-click="AddItem(DataBind)" value="Submit" /><input type="reset" value="Cancel" /></td>
- </tr>
- </tbody>
- </table>
- </body>
-
- </html>
Script Code
- var app = angular.module('myApp', []);
- var listName = "Samplelist";
- app.controller('MainCtrl', function($scope, $http) {
- $scope.Depts = ["Dept1", "Dept2", "Dept3"];
- $scope.AddItem = function(FormData) {
- var data = JSON.stringify(FormData);
- data = data.replace(/[{}]/g, '');
- var datavalue = "{__metadata:{'type':'SP.Data." + listName + "ListItem'}," + data + "}";
- $http({
- url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('" + listName + "')/items",
- method: "POST",
- headers: {
- "Accept": "application/json;odata=verbose",
- "Content-Type": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "X-HTTP-Method": "POST"
- },
- data: datavalue
- }).then(function(response) {
- alert("success");
- }, function(response) {
- alert("failed");
- });
- }
- });
On submit, it will add a record to the list.
Hence, we have created a new record to SharePoint list without mentioning column names using AngularJS.
Conclusion
If someone wants to work on big lists without mentioning all filed names, it will definitely be useful and will save your valuable time.
Here is the full code.
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>CRUD OperationByAJS</title>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
- <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.min.js"></script>
- </head>
-
- <body ng-app="myApp" ng-controller="MainCtrl">
- <table class="table-condensed table-hover tbldata" style="width:100%">
- <thead>CreateForm</thead>
- <tbody>
- <tr>
- <td style="width:10%">Name</td>
- <td style="width:50%"><input class="form-control" ng-model="DataBind.Title" type="text" /></td>
- </tr>
- <tr>
- <td>Salary</td>
- <td style="width:50%"><input class="form-control" ng-model="DataBind.EmpSalary" type="number" /></td>
- </tr>
- <tr>
- <td>Designation</td>
- <td><input class="form-control" ng-model="DataBind.Designation" type="text" /></td>
- </tr>
- <tr>
- <td>Department</td>
- <td><select class="form-control" ng-model="DataBind.Department"><option value="">--Select an option--</option><option ng-repeat="Dept in Depts" value="{{Dept}}">{{Dept}}</option></select></td>
- </tr>
- <tr>
- <td>Address</td>
- <td><textarea class="form-control" ng-model="DataBind.EmpAddress" type="text"></textarea></td>
- </tr>
- <tr>
- <td>Gender</td>
- <td><label><input type="radio" ng-model="DataBind.Gender" value="Male"/>Male</label><label><input type="radio" ng-model="DataBind.Gender" value="Female"/>Female</label></td>
- </tr>
- <tr>
- <td>Employee Id</td>
- <td><input class="form-control" ng-model="DataBind.EmpId" type="number" /></td>
- </tr>
- <tr>
- <td>DOB</td>
- <td><input class="form-control" ng-model="DataBind.DOB" type="date" /></td>
- </tr>
- <tr>
- <td></td>
- <td><input type="button" ng-click="AddItem(DataBind)" value="Submit" /><input type="reset" value="Cancel" /></td>
- </tr>
- </tbody>
- </table>
- </body>
- <script>
- var app = angular.module('myApp', []);
- var listName = "SampleEmplist";
- app.controller('MainCtrl', function($scope, $http) {
- $scope.Depts = ["Dept1", "Dept2", "Dept3"];
- $scope.AddItem = function(FormData) {
- var data = JSON.stringify(FormData);
- data = data.replace(/[{}]/g, '');
- var datavalue = "{__metadata:{'type':'SP.Data." + listName + "ListItem'}," + data + "}";
- $http({
- url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('" + listName + "')/items",
- method: "POST",
- headers: {
- "Accept": "application/json;odata=verbose",
- "Content-Type": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "X-HTTP-Method": "POST"
- },
- data: datavalue
- }).then(function(response) {
- alert("success");
- }, function(response) {
- alert("failed");
- });
- }
- });
- </script>
-
- </html>
It mainly helps when we work with big lists that have more than 100 columns.