In a following post we will see how to add Items to the List
by using REST API in Share Point 2013 On Premises.
Creating list items is little bit complex if you are new to REST API, here we need few
important info
- List Item Type
- REQUEST DIGEST value to prevent replay attacks.
Our Page have two text boxes and submit button. On click of
submit button text box values are inserts into “TestList” (In Mycase).
Please find the below Code Snippet.
<html>
<head>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
var context;
var web;
var Url;
var listItemEntity;
$(document).ready(function () {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', FunctionReady);
ItemSubmit();
});
function ItemSubmit() {
$("#btnSubmit").click(function () {
//reading text box values
var titleValue = $("#title").val();
var columnValue = $("#column").val();
AddItemToList(listItemEntity, titleValue, columnValue);
});
}
function FunctionReady() {
context = new SP.ClientContext.get_current();
web
= context.get_web();
context.load(web);
context.executeQueryAsync(onRequestSuccess, onRequestFailure);
}
function onRequestSuccess() {
Url
= web.get_url();
GetListItemEntity(Url);
}
function onRequestFailure(sender, args) {
alert("Error
Occured:" +
args.get_message());
}
function GetListItemEntity(webUrl) {
var queryUrl = webUrl + "/_api/lists/getbytitle('TestList')?$select=ListItemEntityTypeFullName";
$.ajax({
url: queryUrl,
method: "GET",
headers: { "Accept": "application/json;odata=verbose" },
success: onEntitySuccess,
error: onEntityFailure
});
}
function onEntitySuccess(data) {
listItemEntity = data.d.ListItemEntityTypeFullName;
}
function onEntityFailure(err) {
alert(err.statusText);
}
function AddItemToList(r, titleValue,
colValue) {
try {
var queryUrl = Url + "/_api/lists/GetByTitle('TestList')/items";
$.ajax({
url: queryUrl,
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(
{
'__metadata': {
'type': r
},
'Title': titleValue,
'SampleColumn': colValue
}),
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
}, success: onQuerySucess,
error: onQueryFailure
});
}
catch (ex) {
alert("Exception" + ex.message);
}
}
function onQuerySucess() {
alert('Item
Added Successfully');
}
function onQueryFailure(error) {
//alert("Failure:"+error.status+","+error.statusText);
alert(JSON.stringify(error));
}
</script>
</head>
<body>
Enter Title
value:<input type="textbox" id="title">
<br>
<br>
Enter Column
Value:<input type="textbox" id="column">
<br>
<br>
<input type="button" id="btnSubmit" value="Submit">
</body>
</html>Final Out put :
You can download the same code from the below link,
Download Link:
https://sites.google.com/site/sharepointkitchenblog/AddItem.txt
Regarding REQUEST DIGEST , please check below Url
Hope this helps,
Thank you..!!!
Related Post
SharePoint 2013
- List view render using JSLink with Fabric UI
- Delete Quick Links using Powershell
- delete/Add Top Navigation Links using Powershell
- Get CheckedOut Files from SharePoint 2013 and MOSS2007
- PowerShell::Get Content Database Size in SharePoint
- PowerShell :: Delete All List Items
- Powershell :: Apply Master Page to all sub sites under site collection
- Internet Explorer cannot display this feed - while accessing REST URL in SharePoint
- SharePoint Installation: The tool was unable to install application server role web server (IIS) role SharePoint 2013.
- Basics: Difference between Synchronous Events and Asynchronous Events in SharePoint
- Remove-SPSite 0x80070003 - Forcely delete Site Collection that cannot be deleted
- This operation can be performed only on a computer that is joined to a server farm by users who have permissions in SQL Server to read from the configuration database. To connect this server to the server farm, use the SharePoint Products Configuration Wizard, located on the Start menu in Microsoft SharePoint 2010 Products.
- Restore-SPSite: The operation that you are attempting to perform cannot be completed successfully.
- Migrate Multiple site(sub sites) using MetaLogix and change the Template in Target
- Issue:The date file "C:\ProgramData\Metalogix\EnvironmentSettings.xml" contains invalid data.Values in that file reset to defaults
- Get List Field Type using ECMA Script in SP Hosted Apps
- SharePoint Interview Questions and Answers..
- Issues:SharePoint Foundation 2013 Prerequisites installation(off line) on Windows Server2012 R2
- Get Choice Field Type Values using ECMA Script
- Something Went wrong error message while deploying App in SharePoint 2013
- The Server was unable to save the form at this time. Please try again.
- Get the Items inside the Folder in document Library in SharePoint 2013 using REST API
- SharePoint Client Browser for SharePoint Online and SharePoint On-Premises
- What is Managed meta data service in SharePoint 2010, 2013 and How to Configure it?
- Deprecated features in SharePoint 2013
REST API
- Internet Explorer cannot display this feed - while accessing REST URL in SharePoint
- Get the Items inside the Folder in document Library in SharePoint 2013 using REST API
- XMLHttpRequest: Network Error 0x2eff, Could not complete the operation due to error 00002eff.
- Add Attachment to the List Item Using REST API in SharePoint
- Insert Items into List by using REST API in SharePoint 2013
- Read List Items using REST API
thanks
ReplyDeletesuperb broo!!!....thanks!!!!
ReplyDeleteFirst of all thanks Bro, nowadays I need same so thanks again , you example save my lot of time.
ReplyDeleteEverything is working! Thank you very much!!!
ReplyDeleteHi,
ReplyDeleteThanks for this great help. Could you please suggest how to handle with dropdown field value set in list. Not able to set values in drop down instead of text fields.
Thanks
Great article!!!
ReplyDeletehttp://trickycoder.com
Hi,
ReplyDeleteFirst of all, Thank You for your help. As I am new to this technology, I find it little difficult to understand few terms. It would be a great help if you mail me a explanation of the code or just comment about few functions and methods in the code.
Thanking You
I think the admin of this site is truly working hard for his website, because here every information is quality based stuff.
ReplyDelete