Wednesday, December 3, 2014

SharePoint 2013 Add item to list using JavaScript client object model

In this post we will discuss how to add an item to SharePoint 2013 list using JavaScript object model. In this example we will provide an option for user to enter the title for the item and user can click on the button to save the item.

In my previous post we have discussed about: SharePoint 2013 Disable Windows Authentication prompt, Search Engine Optimization in SharePoint 2013 and New action in SharePoint designer 2013 workflow.

I have a SharePoint list and I want to insert item to that list. We will write the code in a script editor web part.

<script type="text/javascript">
function insertitemtolist()
{
var title=document.getElementById('txtTitle').value;
var clientContext = new SP.ClientContext("http://win-pfcp2dgt8di/sites/EnjoySharePoint/");
var oList = clientContext.get_web().get_lists().getByTitle('TestList1');
var item = new SP.ListItemCreationInformation();
var oListItem = oList.addItem(item);
oListItem.set_item('Title', title);
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
</script>
Enter Item Title: <input type="text" id="txtTitle" /> <br/><br/>
<button type="button" onclick="insertitemtolist();">Insert Item To List</button>

Hope this will help you !!!


Twitter Delicious Facebook Digg Favorites More