Perform Crud Operations in SharePoint 2013 using
REST API.
REST API - I must say
one should really use it when it comes on performance factor in SharePoint
2013. This technology is really light-weighed and off-course easy to use. Every
technology has its own merits and demerits and hence it is our/developers
choice based on requirement/condition what to use and when to use.
If someone asks when we should use REST API, I will
say use REST to perform all most every read operations. We can do Insert,
Update and Delete Operations but one I have encountered an issue which i will
write it in another blog.
here are some examples on REST API Crud Operations.
Below operations are done by me approximate 15
months back and its a humble request to readers to update me if some new
methods or best approaches are available other than this.
1) Read/Get:-
var uspURL = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('ListName')/items";
$.ajax({
url: uspURL,
type: "GET",
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json;odata=verbose"
},
success: function (data) {
var oresults = data.d.results;
},
error: function (jqxr, errorCode, errorThrown) {
alert(jqxr.responseText);
}
});
Some of the useful tips for get are mentioned
below.
Description
|
URL
|
To filter Title column
|
spPageContextInfo.webAbsoluteUrl +
"/_api/web/lists/getbytitle('ListName')/items?$filter=Title
eq’XYZ’";
|
To get Item by ItemID. In this case I assume that
ItemID is ‘1’
|
spPageContextInfo.webAbsoluteUrl +
"/_api/web/lists/getbytitle('ListName')/items(1)";
or spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('ListName')/items?$filter=ID eq’1’"; |
To filter on multiple column
|
spPageContextInfo.webAbsoluteUrl +
"/_api/web/lists/getbytitle('ListName')/items?$filter=(([Title] eq
'ABC') and ([SubTitle] eq 'XYZ'))";
|
To read site columns. Here Site column name is
SiteColumn1
|
spPageContextInfo.webAbsoluteUrl +
"_api/web/fields?$filter=Title eq'SiteColumn1'";
|
To filter Date Column. Here date column is
CreatedDate and operator used is greaterthan
|
spPageContextInfo.webAbsoluteUrl +
"/_api/web/lists/getbytitle('ListName')/items?$filter=CreatedDate
ge datetime'2014-01-01T00:00:00Z'";
|
2) Insert/Add
List Items:-
This insert operation will also add special field
value like DateTime Field, Choice Column and HyperLink Column.
var itemType = "SP.Data.ListNameListItem";
var arrChoiceColumn = [];
arrChoiceColumn.push('Choice1');
arrChoiceColumn.push('Choice2');
var vardata = {
'__metadata': { 'type': itemType },
'Title': 'TestTitle', 'ChoiceColumName': {
"__metadata": { "type": "Collection(Edm.String)" },
"results": arrChoiceColumn
},
'DateColumnName': new Date().toISOString(),
'HyperLinkColumnName': {
'__metadata': { 'type': 'SP.FieldUrlValue' },
'Description': 'Azam Khan', 'Url': 'http://khanaazam.blogspot.ae/'
}
};
var uspURL = webURL + "/_api/web/lists/getbytitle('ListName')/items";
$.ajax({
url: uspURL, type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(vardata),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
}, success: function (data) {
// here add alert message
},
error: function (jqxr, errorCode, errorThrown) {
alert(jqxr.responseText);
}
});
3) Update
List Items:-
While updating any records
using REST in SharePoint 2013 it is same as Add Operation with some changes
which are mentioned below:-
a) Since we are
updating list item it is basic that first we need to get that item so for that
I will use get Item By Id method and hence the URL will be
var url = spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('ListName')/items(1)";
b) Second change will
be in header of Ajax Call
headers:
{
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "MERGE","If-Match": "*"
}
c) In Data we will
pass the Column Name along with their value the way how we have passed in the
Add Operation.
3) Delete
List Items:-
For Delete operation we
can use the everything similar to Update Query with the only difference in
headers
headers:
{
"Accept": "application/json; odata=verbose",
"content-type": "application/json; odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"IF-MATCH": "*","X-HTTP-Method": "DELETE"
}
Hi, Nice post. It would have been better if you provide examples also for each of these operations for instance.. for GET operation please provide how to loop thru oresults and bind data to controls.
ReplyDeleteHi Sudhakar,
ReplyDeleteThanks for sharing your valuable comments.
In my next post definitely i will provide examples.
Keep visiting my blogs and provide your valuable comments.