Add an Item SharePoint REST API
<script type="text/javascript">
function createItem() {
var addNewItemUrl = "/_api/Lists/GetByTitle('Customer1')/Items";
var itemType = GetItemTypeForListName('Customer1');
var item = {
"__metadata": { "type": itemType },
"Title": $("#cTitle").val(),
"FirstName": $("#cFirstName").val(),
"LastName": $("#cLastName").val(),
"Location": $("#cLocation").val(),
"PhNumber": $("#cPhone").val()
};
addNewItem(addNewItemUrl, item);
}
function addNewItem(url, data) {
$.ajax({
url:
_spPageContextInfo.webAbsoluteUrl + url,
type: "POST",
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"content-Type": "application/json;odata=verbose"
},
data: JSON.stringify(data),
success: function (data) {
alert("Record Added");
},
error: function (error) {
console.log(JSON.stringify(error));
}
});
}
function GetItemTypeForListName(name) {
return "SP.Data." + name.charAt(0).toUpperCase() + name.slice(1) + "ListItem";
}
</script>
===========================================
======================================
===========================================
------------------------------------
Some of OData queries:
//Not working -->$apply=groupby(Category)
_api/Lists/GetByTitle('Informations')/items?$apply=groupby(Category)
_api/Lists/GetByTitle('Informations')/items?$select=EncodedAbsUrl,Category,ID&$orderby=Category asc&$orderby=Modified desc
//Get only files, not a folder type= $filter=FSObjType eq 0
_api/web/lists/getbytitle('Informations')/items?$select=EncodedAbsUrl,Category&$apply=groupby(Category)&$orderby=Category asc &$filter=FSObjType eq 0
/_api/Lists/GetByTitle('Informations')/items?$select=EncodedAbsUrl,Category,ID&$orderby=Category asc&$orderby=Modified desc
Update an Item SharePoint REST API
function updateItem() {
var itemid = 4;
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Customer1')/getItemById(" + itemid + ")";
var itemType = GetItemTypeForListName('Customer1');
var item = {
"__metadata": { "type": itemType },
"Title": 'Ms',
"FirstName": 'Janu',
"LastName": 'Lov',
"Location": 'Sec',
"PhNumber": '765'
};
UpdateData(requestUri, item);
}
function UpdateData(requestUri, item) {
$.ajax({
url: requestUri,
type: "POST",
data: JSON.stringify(item),
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "MERGE",
"IF-MATCH": "*"
},
success: onSuccess,
error: onError
});
function onSuccess(data) {
alert("Updated");
}
function onError(error) {
console.log(JSON.stringify(error));
}
}
function GetItemTypeForListName(name) {
return "SP.Data." + name.charAt(0).toUpperCase() + name.slice(1) + "ListItem";
}
Delete an Item SharePoint REST API
function deleteItem() {
var itemid = 6;
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Customer1')/getItemById(" + itemid + ")";
DeleteItem(requestUri);
}
function DeleteItem(deleteuri) {
$.ajax({
url: deleteuri,
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: onDeleteSuccess,
error: onDeleteError
});
}
function onDeleteSuccess() {
alert("Entry
Deleted");
}
function onDeleteError() {
console.log(JSON.stringify(error));
}
function GetItemTypeForListName(name) {
return "SP.Data." + name.charAt(0).toUpperCase() + name.slice(1) + "ListItem";
}
<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"> </script>
<script type="text/javascript" src="/_layouts/15/sp.js"> </script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">
<div id="createItem">
<table>
<tr>
<td>
<table>
<tr>
<td>Title</td>
<td>
<input type="text" id="cTitle" />
</td>
</tr>
<tr>
<td>First Name</td>
<td>
<input type="text" id="cFirstName" />
</td>
</tr>
<tr>
<td>Last Name</td>
<td>
<input type="text" id="cLastName" />
</td>
</tr>
<tr>
<td>Location</td>
<td>
<input type="text" id="cLocation" />
</td>
</tr>
<tr>
<td>Phone</td>
<td>
<input type="text" id="cPhone" />
</td>
</tr>
<tr>
<td>
<button value="Add" id="btnAddNewItem" onclick="createItem();">Add</button>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
Some of OData queries:
//Not working -->$apply=groupby(Category)
_api/Lists/GetByTitle('Informations')/items?$apply=groupby(Category)
_api/Lists/GetByTitle('Informations')/items?$select=EncodedAbsUrl,Category,ID&$orderby=Category asc&$orderby=Modified desc
//Get only files, not a folder type= $filter=FSObjType eq 0
_api/web/lists/getbytitle('Informations')/items?$select=EncodedAbsUrl,Category&$apply=groupby(Category)&$orderby=Category asc &$filter=FSObjType eq 0
/_api/Lists/GetByTitle('Informations')/items?$select=EncodedAbsUrl,Category,ID&$orderby=Category asc&$orderby=Modified desc
//Filtering with two field types
/_api/Lists/GetByTitle('CTRTEM-001245')/Items?$filter=FSObjType eq 0 and Type eq 'TypeFieldVal'
No comments:
Post a Comment