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
- Open your SharePoint Site.
- Edit the SharePoint page and add a Content Editor WebPart.
- Add the following JavaScript code into CEWP.
- Save the page and hit F5.
Code snippet
- $(document).ready(function
- {
- updatelistitem();
- });
-
- function updatelistitem() {
-
- var clientContext = new SP.ClientContext(https:
- this.oListItem = oList.getItemById(3);
- var oList = clientContext.get_web().get_lists().getByTitle('Announcements');
- oListItem.set_item('Title', 'My Updated Title');
-
- oListItem.update();
-
- }
-
- function onQuerySucceeded() {
-
- alert('Item updated sucessfully: ' + oListItem.get_id());
- }
-
- function onQueryFailed(sender, args) {
-
- alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
- }