In this article, I have explained how to perform create, read, update, and delete operations on SharePoint list items using HTTP requests.
HTTP Request |
Use |
GET |
This method helps to fetch the information fromSharepoint list |
POST |
This method helps to create or update the list items in sharepoint listPUT: Required all the object properties to update the resourcesMerge: Optional to required all the object properties to update the resources |
PUT/MERGE |
This method helps to update the existing object using X-HTTP method |
DELETE |
This method helps to delete the object from sharepoint list |
Open sharepoint site,
Create a list name “Employee” to play Create, Read, Update, Delete Operations using HTTP RequestsP
“POST” method used to create Item in the sharepoint list
URL
http://<Sharepoint SiteURL>/_api/web/lists/Getbytitle(listname)/items
- function createListItem() {
-
- var eName = $('#txtempname').val();
- var eDesg = $('#txtdesignation').val();
- var eEmail = $('#txtemail').val();
- var eMobile = $('#txtmobile').val();
- var eBloodGroup = $('#txtbloodgrp').val();
- var eComAddress = $('#txtaddress').val();
- var eEmergency = $('#txtemergency').val();
-
- $.ajax({
- async: true,
-
- url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Employee')/items",
- method: "POST",
- data: JSON.stringify({
- '__metadata': {
- 'type': 'SP.Data.EmployeeListItem'
- },
- //Pass the parameters
- 'EmployeeName': eName,
- 'Designation': eDesg,
- 'Email': eEmail,
- 'Mobile': eMobile,
- 'BloodGroup': eBloodGroup,
- 'CommunicationAddress': eComAddress,
- 'EmergencyContact': eEmergency
- }),
- headers: {
- "accept": "application/json;odata=verbose",
- "content-type": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val()
- },
- success: function(data) {
- swal("Item created successfully", "success");
- },
- error: function(error) {
- console.log(JSON.stringify(error));
-
- }
-
- })
-
- }
"GET" Method used to Fetch the list items from the sharepoint list
URL
http://<Sharepoint SiteURL>/_api/web/lists/Getbytitle(listname)/items
- function getItems() {
-
- $.ajax({
-
- async: true,
- url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Employee')/items",
- method: "GET",
-
- headers: {
- "accept": "application/json;odata=verbose",
- "content-type": "application/json;odata=verbose"
-
- },
- success: function(data) {
- data = data.d.results;
- //Iterate the data
- $.each(data, function(index, value) {
-
- var html = "<tr><td>" + value.EmployeeName + "</td><td>" + value.Designation + "</td><td>" + value.Email + "</td>
- <td>" + value.BloodGroup + "</td><td>" + value.CommunicationAddress + "</td>
- <td>" + value.EmergencyContact + "</td><td>" + value.Mobile + "</td>
- <td>
- <a href='#' data-target='#ModalForUpdateEmployee' data-toggle='modal' onclick='edit(" + value.Id + ")'>
- <img src='https://sharepointtechie.sharepoint.com/sites/automatedwiki/SiteAssets/CRUD/003-edit-document.png'>
- </a></td><td><a href='#' onclick='deleteItem(" + value.Id + ");'>
- <img src='https://sharepointtechie.sharepoint.com/sites/automatedwiki/SiteAssets/CRUD/001-delete.png'></a></td>
- </tr>";
- $('.table tbody').append(html); //Append the HTML
-
- });
-
- table = $('#subsiteList').DataTable(); //initialize the datatable
- },
- error: function(error) {
- console.log(JSON.stringify(error));
-
- }
-
- })
-
-
- }
"MERGE" Method used to perform update the item in sharepoint list
URL
http://<Sharepoint SiteURL>/_api/web/lists/Getbytitle(listname)/items(itemid)
- function update(uId) {
- //Fetch the values from the input elements
- var eName = $('#txtempnames').val();
- var eDesg = $('#txtdesignations').val();
- var eEmail = $('#txtemails').val();
- var eMobile = $('#txtmobiles').val();
- var eBloodGroup = $('#txtbloodgrps').val();
- var eComAddress = $('#txtaddresss').val();
- var eEmergency = $('#txtemergencys').val();
-
- $.ajax({
-
-
- url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Employee')/items(" + uId + ")",
- method: "POST",
- data: JSON.stringify({
- '__metadata': {
- 'type': 'SP.Data.EmployeeListItem'
- },
- 'EmployeeName': eName,
- 'Designation': eDesg,
- 'Email': eEmail,
- 'Mobile': eMobile,
- 'BloodGroup': eBloodGroup,
- 'CommunicationAddress': eComAddress,
- 'EmergencyContact': eEmergency
- }),
- headers: {
- "accept": "application/json;odata=verbose",
- "content-type": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "IF-MATCH": "*", //Overrite the changes in the sharepoint list item
- "X-HTTP-Method": "MERGE" // Specifies the update operation in sharepoint list
- },
- success: function(data) {
- swal( "Item Updated successfully", "success");
- //Reninitialize the datatable
- if ($.fn.DataTable.isDataTable('#subsiteList')) {
- $('#subsiteList').DataTable().destroy();
- }
- $('#subsiteList tbody').empty();
-
- //Bind the data into datatable
- getItems();
- },
- error: function(error) {
- console.log(JSON.stringify(error));
-
- }
-
- })
-
-
- }
"DELETE" method used to perfoms delete items in the sharepoint list
URL
http://<Sharepoint SiteURL>/_api/web/lists/Getbytitle(listname)/items(itemid)
- function deleteItem(value) {
-
- $.ajax({
-
-
- url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Employee')/items(" + value + ")",
- method: "POST",
- headers: {
- "accept": "application/json;odata=verbose",
- "content-type": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "IF-MATCH": "*",
- "X-HTTP-Method": "DELETE"
- },
- success: function(data) {
-
- swal("Deleted!", "Item Deleted successfully", "success");
-
- if ($.fn.DataTable.isDataTable('#subsiteList')) {
- $('#subsiteList').DataTable().destroy();
- }
- $('#subsiteList tbody').empty();
-
-
- getItems();
- },
- error: function(error) {
- console.log(JSON.stringify(error));
-
- }
-
- })
-
-
- }
- $(document).ready(function() {
-
- getItems();
-
- });
-
-
-
- function createListItem() {
- var eName = $('#txtempname').val();
- var eDesg = $('#txtdesignation').val();
- var eEmail = $('#txtemail').val();
- var eMobile = $('#txtmobile').val();
- var eBloodGroup = $('#txtbloodgrp').val();
- var eComAddress = $('#txtaddress').val();
- var eEmergency = $('#txtemergency').val();
-
- $.ajax({
-
- async: true,
- url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Employee')/items",
- method: "POST",
- data: JSON.stringify({
- '__metadata': {
- 'type': 'SP.Data.EmployeeListItem'
- },
- 'EmployeeName': eName,
- 'Designation': eDesg,
- 'Email': eEmail,
- 'Mobile': eMobile,
- 'BloodGroup': eBloodGroup,
- 'CommunicationAddress': eComAddress,
- 'EmergencyContact': eEmergency
- }),
- headers: {
- "accept": "application/json;odata=verbose",
- "content-type": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val()
- },
- success: function(data) {
-
- var eName = $('#txtempname').val("");
- var eDesg = $('#txtdesignation').val("");
- var eEmail = $('#txtemail').val("");
- var eMobile = $('#txtmobile').val("");
- var eBloodGroup = $('#txtbloodgrp').val("");
- var eComAddress = $('#txtaddress').val("");
- var eEmergency = $('#txtemergency').val("");
-
- swal("Item created successfully", "success");
-
- if ($.fn.DataTable.isDataTable('#subsiteList')) {
- $('#subsiteList').DataTable().destroy();
- }
- $('#subsiteList tbody').empty();
-
-
- getItems();
- },
- error: function(error) {
- console.log(JSON.stringify(error));
-
- }
-
- })
-
- }
-
-
- function getItems() {
-
- $.ajax({
-
- async: true,
- url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Employee')/items",
- method: "GET",
-
- headers: {
- "accept": "application/json;odata=verbose",
- "content-type": "application/json;odata=verbose"
-
- },
- success: function(data) {
- data = data.d.results;
- console.log(data);
- $.each(data, function(index, value) {
-
- var html = "<tr><td>" + value.EmployeeName + "</td><td>" + value.Designation + "</td><td>" + value.Email + "</td><td>" + value.BloodGroup + "</td><td>" + value.CommunicationAddress + "</td><td>" + value.EmergencyContact + "</td><td>" + value.Mobile + "</td><td><a href='#' data-target='#ModalForUpdateEmployee' data-toggle='modal' onclick='edit(" + value.Id + ")'><img src='https://sharepointtechie.sharepoint.com/sites/automatedwiki/SiteAssets/CRUD/003-edit-document.png'></a></td><td><a href='#' onclick='deleteItem(" + value.Id + ");'><img src='https://sharepointtechie.sharepoint.com/sites/automatedwiki/SiteAssets/CRUD/001-delete.png'></a></td></tr>";
- $('.table tbody').append(html);
-
- });
-
- table = $('#subsiteList').DataTable();
- },
- error: function(error) {
- console.log(JSON.stringify(error));
-
- }
-
- })
-
-
- }
-
- function edit(value) {
-
- $.ajax({
-
- async: true,
- url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Employee')/GetItemByID(" + value + ")",
- method: "GET",
-
- headers: {
- "accept": "application/json;odata=verbose",
- "content-type": "application/json;odata=verbose"
-
- },
- success: function(data) {
- console.log(data.d.EmployeeName);
- eName = $('#txtempnames').val(data.d.EmployeeName);
- eDesg = $('#txtdesignations').val(data.d.Designation);
- eEmail = $('#txtemails').val(data.d.Email);
- eMobile = $('#txtmobiles').val(data.d.Mobile);
- eBloodGroup = $('#txtbloodgrps').val(data.d.BloodGroup);
- eComAddress = $('#txtaddresss').val(data.d.CommunicationAddress);
- eEmergency = $('#txtemergencys').val(data.d.EmergencyContact);
-
-
-
- },
- error: function(error) {
- console.log(JSON.stringify(error));
-
- }
-
-
- })
-
- uId = value;
- }
-
-
-
-
- function update(uId) {
- var eName = $('#txtempnames').val();
- var eDesg = $('#txtdesignations').val();
- var eEmail = $('#txtemails').val();
- var eMobile = $('#txtmobiles').val();
- var eBloodGroup = $('#txtbloodgrps').val();
- var eComAddress = $('#txtaddresss').val();
- var eEmergency = $('#txtemergencys').val();
-
- $.ajax({
-
-
- url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Employee')/items(" + uId + ")",
- method: "POST",
- data: JSON.stringify({
- '__metadata': {
- 'type': 'SP.Data.EmployeeListItem'
- },
- 'EmployeeName': eName,
- 'Designation': eDesg,
- 'Email': eEmail,
- 'Mobile': eMobile,
- 'BloodGroup': eBloodGroup,
- 'CommunicationAddress': eComAddress,
- 'EmergencyContact': eEmergency
- }),
- headers: {
- "accept": "application/json;odata=verbose",
- "content-type": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "IF-MATCH": "*",
- "X-HTTP-Method": "MERGE"
- },
- success: function(data) {
- swal("Item Updated successfully", "success");
-
- if ($.fn.DataTable.isDataTable('#subsiteList')) {
- $('#subsiteList').DataTable().destroy();
- }
- $('#subsiteList tbody').empty();
-
-
- getItems();
- },
- error: function(error) {
- console.log(JSON.stringify(error));
-
- }
-
- })
-
-
- }
-
-
-
- function deleteItem(value) {
-
- $.ajax({
-
-
- url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Employee')/items(" + value + ")",
- method: "POST",
- headers: {
- "accept": "application/json;odata=verbose",
- "content-type": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "IF-MATCH": "*",
- "X-HTTP-Method": "DELETE"
- },
- success: function(data) {
-
- swal("Deleted!", "Item Deleted successfully", "success");
-
- if ($.fn.DataTable.isDataTable('#subsiteList')) {
- $('#subsiteList').DataTable().destroy();
- }
- $('#subsiteList tbody').empty();
-
-
- getItems();
- },
- error: function(error) {
- console.log(JSON.stringify(error));
-
- }
-
- })
-
-
- }
just add the content editor into the page and link the HTML file
Output
So in this article you learned how to create, read, update, delete operations using HTTP Request
Happy Sharepointing!.....