Ans:-
Get List Items
In order to get all items from the list, below code can be used-
$.ajax({
var siteurl = _spPageContextInfo.webAbsoluteUrl;
$.ajax({
url: siteurl + "/_api/web/lists/getbytitle('Projects')/items",
type: "GET",
headers: {
"Accept": "application/json; odata=verbose"
},
success: function (data) {
if (data.d.results.length > 0 ) {
//This section can be used to iterate through data and show it on screen
}
},
error: function (data) {
alert("Error: "+ data);
}
});
});
Add New List Item
In order to add new list item, we have to create a data variable and assign values to the fields, then perform a 'POST' request. See below code-
var data = {
__metadata: { 'type': 'SP.Data.ProjectsListItem' },
Title: 'Please provide title here',
Description: 'Please provide multi line text here',
Start_x0020_Date: new Date().toISOString();
};
$.ajax({
var siteurl = _spPageContextInfo.webAbsoluteUrl;
$.ajax({
url: siteurl + "/_api/web/lists/getbytitle('Projects')/items",
type: "POST",
data: JSON.stringify(data),
headers: {
"Accept": "application/json; odata=verbose",
"Content-Type": "application/json; odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
alert('Item added successfully');
},
error: function (error) {
alert("Error: "+ JSON.stringify(error));
}
});
});
Update List Item
In order to update a list item, we have to pass Item Id in Url, create data variable and use one of the HTTP methods - MERGE, PATCH or PUT. Preferred or recommended methods are PATCH or MERGE a PUT requires all fields to be sent as an object.
This is similar to adding a new item with few minor changes.
var data = {
__metadata: { 'type': 'SP.Data.ProjectsListItem' },
Title: 'Please provide title here',
Description: 'Please provide multi line text here',
Start_x0020_Date: new Date().toISOString();
};
$.ajax({
var siteurl = _spPageContextInfo.webAbsoluteUrl;
$.ajax({
url: siteurl + "/_api/web/lists/getbytitle('Projects')/items/getbyId(3)",
type: "POST",
data: JSON.stringify(data),
headers: {
"accept":"application/json;odata=verbose",
"content-Type":"application/json;odata=verbose",
"X-RequestDigest":$('#__REQUESTDIGEST').val(),
"X-HTTP-Method":"MERGE",
"IF-Match":"*",
},
success: function (data) {
alert('Item updated successfully');
},
error: function (error) {
alert("Error: "+ JSON.stringify(error));
}
});
});
Delete Specific List Item
In order to delete a specific item from the list, we have to make few changes and do some addition to the code. See below code-
$.ajax({
var siteurl = _spPageContextInfo.webAbsoluteUrl;
$.ajax({
url: siteurl + "/_api/web/lists/getbytitle('Projects')/items/getbyId(1)",
type: "POST",
headers: {
"accept":"application/json;odata=verbose",
"content-Type":"application/json;odata=verbose",
"X-RequestDigest":$('#__REQUESTDIGEST').val(),
"X-HTTP-Method":"DELETE",
"If-Match":"*",
},
success: function (data) {
alert('Item deleted successfully');
},
error: function (error) {
alert("Error: "+ JSON.stringify(error));
}
});
});
No comments:
Post a Comment