Create List Item in SharePoint using JSOM

The example in this topic show how to use JSOM to Create List Item in SharePoint

  • function CreateListItem() {
  •     // You can optionally specify the Site URL here to get the context
  •     // If you don't specify the URL, the method will get the context of the current site
  •     // var clientContext = new SP.ClientContext("http://MyServer/sites/SiteCollection");
  •     var clientContext = new SP.ClientContext();
  •  
  •     var oWeb = clientContext.get_web();
  •  
  •     // Specify list title here
  •     var oList = oWeb.get_lists().getByTitle("List Name");
  •  
  •     // Get Item using CAML Query
  •     var camlQuery = new SP.CamlQuery();
  •  
  •     // New "ListItemCreationInformation" Object
  •     var oListItemCreationInformation = new SP.ListItemCreationInformation();
  •  
  •     var oListItem = oList.addItem(oListItemCreationInformation);
  •  
  •     // Set value for each column here
  •     oListItem.set_item('Title', 'New item value');
  •     oListItem.set_item('Notes', 'This is dummy data');
  •  
  •     oListItem.update();
  •  
  •     clientContext.load(oListItem);
  •  
  •     // Execute the query to the server.
  •     clientContext.executeQueryAsync(onsuccess, onfailed);
  • }
  •  
  • function onsuccess() {
  •     console.log("Success");
  • }
  •  
  • function onfailed(sender, args) {
  •     console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
  • }

Thank you for reading this article. This code was tested in SharePoint 2013


© 2019 Code SharePoint