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

In this blog, I would like to add a code snippet for updating items in a 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. updatelistitem();  
  4. });  
  5.   
  6. function updatelistitem() {  
  7.   
  8.     var clientContext = new SP.ClientContext(https://gowtham.sharepoint.com/tutorials");  
  9.     this.oListItem = oList.getItemById(3);  
  10.     var oList = clientContext.get_web().get_lists().getByTitle('Announcements');  
  11.     oListItem.set_item('Title''My Updated Title');  
  12.   
  13.     oListItem.update();  
  14.   
  15.     }  
  16.   
  17. function onQuerySucceeded() {  
  18.   
  19.     alert('Item updated sucessfully: ' + oListItem.get_id());  
  20. }  
  21.   
  22. function onQueryFailed(sender, args) {  
  23.   
  24.     alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
  25. }  
Ebook Download
View all
Learn
View all