SharePoint List Operations on SharePoint 2013 using REST API Services In - Part I (ADD)
Add List Item
function AddItem()
{
var titleVal = $("#txtTitle").val();
var metadataVal = {
"__metadata": { "type": getListItemType("SharePoint List Name") },
"Title": titleVal
};
$.ajax
({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('SharePoint List Name')/items",
type: "POST",
data: JSON.stringify(metadataVal),
headers:
{
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "POST"
},
success: function(data, status, xhr)
{
//alert("Success");
},
error: function(xhr, status, error)
{
//Failure
}
});
}
function getListItemType(name) {
return"SP.Data." + name[0].toUpperCase() + name.substring(1) + "ListItem";
}
function AddItem()
{
var titleVal = $("#txtTitle").val();
var metadataVal = {
"__metadata": { "type": getListItemType("SharePoint List Name") },
"Title": titleVal
};
$.ajax
({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('SharePoint List Name')/items",
type: "POST",
data: JSON.stringify(metadataVal),
headers:
{
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "POST"
},
success: function(data, status, xhr)
{
//alert("Success");
},
error: function(xhr, status, error)
{
//Failure
}
});
}
function getListItemType(name) {
return"SP.Data." + name[0].toUpperCase() + name.substring(1) + "ListItem";
}
Comments
Post a Comment