SharePoint - CRUD Operations with REST API

Create Read Update Delete
1) SharePoint - Create New Record
To insert a new record from user inputs to a SharePoint list, we need a REST API as a medium between the page and the SharePoint data base.
We are using a REST API call to insert a new record into SharePoint list from a webpage.
Use below code to create a new record to a SharePoint list.

var URL = _spPageContextInfo.webAbsoluteUrl;
$.ajax({  
    url:  URL,  
    type: "POST",  
    headers: {  
        "accept": "application/json;odata=verbose",  
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
        "content-Type": "application/json;odata=verbose"  
    },  
    data: "{
	__metadata:{'type':'SP.Data.YourlistnameListItem'}, 
	Title:"input value"
	}",
	// You can add more input with diffrent column name. here "Title" is internal name in sharepoint list.
    success: function(data) {  
		// inside data.d.results you will get object for created item.
        console.log(data.d.results);  
    },  
    error: function(error) { 
		// if you receive any error please check list internal name and column internal names.
        console.log(JSON.stringify(error));  
    }  
});

2) SharePoint - Read record
To retrive list items from SharePoint list, we need a REST API as a medium between the SharePoint data base and page.
We are using a REST API call to retrive SharePoint list items from a SharePoint database.
We can retrieve the data from SharePoint list and we can display it in webpage.

var URL = _spPageContextInfo.webAbsoluteUrl;
$.ajax({  
    url: URL + "/_api/web/lists/GetByTitle('List Name')/items",  
    type: "GET",  
    headers: {  
        "accept": "application/json;odata=verbose",  
        "content-Type": "application/json;odata=verbose"  
    },  
    success: function(data) {  
		// inside data.d.results you will get object for sharepoint list item(s).
        console.log(data.d.results);  
    },  
    error: function(error) {  
		// if you receive any error please check list column internal names and URL with proper notation.
        console.log(JSON.stringify(error));  
    }  
});  

3) SharePoint - Update record
To update SharePoint list item, we need a REST API as a medium between the page and SharePoint data base.
We are using a REST API call to update a existing record into SharePoint list from a webpage.
Use below code to update a existing record into an SharePoint list.

var URL = _spPageContextInfo.webAbsoluteUrl;
$.ajax({  
    url: URL + "/_api/web/lists/GetByTitle('List Name')/items(Item ID)",  
    type: "POST",  
    headers: {  
        "accept": "application/json;odata=verbose",  
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
        "content-Type": "application/json;odata=verbose",  
        "IF-MATCH": "*",  
        "X-HTTP-Method": "MERGE"  
    },  
    data: "{
	__metadata:{'type':'SP.Data.YourlistnameListItem'},
	Title:"Your new input "}",  
    // You can add more input with diffrent column name. here "Title" is internal name in sharepoint list.
    success: function(data) {  
        console.log(data.d.results);  
    },  
    error: function(error) {  
		// if you receive any error please check list column internal names and URL with proper notation.
        console.log(JSON.stringify(error));  
    }  
}); 

4) SharePoint - Delete record
To delete SharePoint list item, we need a REST API to delete item from sharepoint list.
We are using a REST API call to delete a existing record from SharePoint list.
Use below code to delete a exiting record from an existing SharePoint list item.

var URL = _spPageContextInfo.webAbsoluteUrl;
$.ajax({  
    url: URL + "/_api/web/lists/GetByTitle('List Name')/items(Item ID)",  
    type: "POST",  
    async: false,  
    headers: {  
        "Accept": "application/json;odata=verbose",  
        "X-Http-Method": "DELETE",  
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
        "If-Match": "*"  
    },  
    success: function(data) {  
        console.log('item deleted successfully');  
    },  
    error: function(data) {  
        console.log('Item can not delete');  
    }  
});  

#RM #SharePoint #REST API #CRUD #Create #Get #Update #Delete #Retrive #Insert #Fetch
#ajax php #ajax jquery #ajax call #sharepoint database #ajax data #ajax javascript #ajax jquery php #ajax jquery example #ajax success #ajax request #php ajax post #ajax url #javascript ajax call #ajax call jquery #ajax php example #ajax html #ajax and php #ajax function #ajax method #ajax php form #ajax website #ajax call example #ajax jquery example php #ajax form #ajax call using jquery

Comments

Popular posts from this blog

SharePoint REST API

Hide "Recurrence" & "All Day Event" from Event content type in SharePoint