Add A List Item In SharePoint Using JSOM (Announcement List)

In this blog, I would like to share a code snippet for how to delete an item in SharePoint announcement list, using JavaScript Object model.

Steps
  1. Open your SharePoint site.
  2. Edit SharePoint page and add a Content Editor WebPart.
  3. Add JavaScript code given below into CEWP.
  4. Save the page and hit F5.
Code snippet
  1. $(document).ready(function  
  2. {  
  3. deletelistitem();  
  4. });  
  5.   
  6. function deletelistitem() {  
  7.   
  8.     var clientContext = new SP.ClientContext(https://gowtham.sharepoint.com/tutorials");  
  9.     var oList = clientContext.get_web().get_lists().getByTitle('Announcements');          
  10.     var itemCreateInfo = new SP.ListItemCreationInformation();  
  11.     this.oListItem = oList.addItem(itemCreateInfo);          
  12.     oListItem.set_item('Title''Test Item!');  
  13.     oListItem.set_item('Body''This Item has added Programmatically!');          
  14.     oListItem.update();  
  15.     clientContext.load(oListItem);          
  16.     clientContext.executeQueryAsync(Function.createDelegate(thisthis.onQuerySucceeded), Function.createDelegate(thisthis.onQueryFailed));  
  17. }  
  18.   
  19. function onQuerySucceeded() {  
  20.   
  21.     alert('Item created sucessfully: ' + oListItem.get_id());  
  22. }  
  23.   
  24. function onQueryFailed(sender, args) {  
  25.   
  26.     alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
  27. }