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
- 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{
-
- createListItem();
- });
-
- function createListItem() {
-
- var clientContext = new SP.ClientContext(https:
- var oList = clientContext.get_web().get_lists().getByTitle('Announcements');
- this.oListItem = oList.getItemById(7);
-
- oListItem.deleteObject();
-
- clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
- }
-
-
- function onQuerySucceeded() {
-
- alert('Item created sucessfully: ' + oListItem.get_id());
- }
-
- function onQueryFailed(sender, args) {
-
- alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
- }
Thanks for reading my blog.