Wednesday 8 January 2014

Add item into SharePoint list using javascript

In this blog, we learn how to add item into SharePoint list using JavaScript.

Below function add to the content editor webpart or into your core .js file,and call on link click or button click.

<script type="text/javascript">
// Make sure the SharePoint script file 'sp.js' is loaded before your
// code runs.
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);
function createListItem()
{
    var clientContext = SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('InwardOutward');
    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);
   oListItem.set_item('Title', 'My New Item!');
   oListItem.update();
    clientContext.load(oListItem);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded()
{
    alert('Item created: ' + oListItem.get_id());
 }
function onQueryFailed(sender, args)
{
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>

<a href="javascript:createListItem();"><img alt="Add" src="/Image_/add.png" border="0"/></a>

While using JavaScript to add item into list following error may occurred:

3 comments:

  1. Thanks website design... :)

    ReplyDelete
  2. is there a way to add multiple values to a list?

    ReplyDelete
    Replies
    1. multiple values as in multiple columns :D
      Thanks

      Delete