Add item to SharePoint list using JavaScript object model in SharePoint Online

InfoPath alternatives for form designing SharePoint

In this post we will discuss how we can add an item to list using JavaScript object model. We also use jQuery here. You can use the code inside a script editor web part to make it work.

<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js”></script>

<input id=”txtTitle” name=”txtTitle” type=”text” />

<input name=”ADD” id=”btnAdd” type=”submit” value=”Add Item to List” />

<script language=”javascript” type=”text/javascript”>

$(document).ready(function () {

$(“#btnAdd”).click(function () {
var title = $(“#txtTitle”).val();
AddListItem(title);
});
});
function AddListItem(TitleField) {
var ListName = “Employee”;
var context = new SP.ClientContext.get_current();
var lstObject = context.get_web().get_lists().getByTitle(ListName);
var listItemCreationInfo = new SP.ListItemCreationInformation();
var newItem = lstObject.addItem(listItemCreationInfo);
newItem.set_item(‘Title’, TitleField);
newItem.update();
context.executeQueryAsync(Function.createDelegate(this, this.onSuccess),
Function.createDelegate(this, this.onFailure));
}
function onSuccess() {
alert(‘Item created’);
}
function onFailure(sender, args) {
alert(‘Request failed. ‘ + args.get_message() + ‘\n’ + args.get_stackTrace());
}
</script>

Hope this will be helpful.

Similar SharePoint 2013 Tutorials

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →

Leave a Reply