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

In this blog, I would like to add a code snippet for adding a new item in SharePoint Announcement list, using JavaScript Object Model. The Client Object Model must conclude with a call to ExecuteQuery() or  ExecuteQueryAsync (ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler).
 
Steps
  1. Open your SharePoint Site.
  2. Edit the SharePoint page and add a Content Editor WebPart .
  3. Add the following JavaScript code into CEWP.
  4. Save the page and hit F5.
Code snippet
  1. $(document).ready(function{  
  2.   
  3. createListItem();  
  4. });  
  5.   
  6. function createListItem() {  
  7.   
  8.     var clientContext = new SP.ClientContext(https://gowtham.sharepoint.com/tutorials");  
  9.       var oList = clientContext.get_web().get_lists().getByTitle('Announcements');  
  10.     this.oListItem = oList.getItemById(7);  
  11.   
  12.     oListItem.deleteObject();  
  13.   
  14.     clientContext.executeQueryAsync(Function.createDelegate(thisthis.onQuerySucceeded), Function.createDelegate(thisthis.onQueryFailed));  
  15. }  
  16.   
  17.   
  18. function onQuerySucceeded() {  
  19.   
  20.     alert('Item created sucessfully: ' + oListItem.get_id());  
  21. }  
  22.   
  23. function onQueryFailed(sender, args) {  
  24.   
  25.     alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
  26. }  
Thanks for reading my blog.
Ebook Download
View all
Learn
View all