sharepoint 2010 write to list using ecmascript


/ Published in: JavaScript
Save to your folder(s)

This code works for me without requiring any includes, plugins, or service packs. It should also integrate seamlessly with jquery.


Copy this code and paste it in your HTML
  1. <script type="text/javascript">
  2.  
  3. ExecuteOrDelayUntilScriptLoaded(functionName, "sp.js"); //this is necessary to ensure the library is loaded before function triggered
  4.  
  5.  
  6. function functionName() {
  7.  
  8.  
  9. var clientContext = SP.ClientContext.get_current();
  10. var myList = clientContext.get_web().get_lists().getByTitle('myList'); //actual list name here
  11.  
  12. var itemCreationInfo = new SP.ListItemCreationInformation();
  13. var listItem = myList.addItem(itemCreationInfo);
  14.  
  15. listItem.set_item("Title", "the title"); //actual field names go in first argument, new assigned value in second
  16. listItem.set_item("Field2", "tuna");
  17. listItem.set_item("Field3", "ape");
  18. listItem.set_item("Field4", "moose");
  19.  
  20.  
  21. listItem.update();
  22.  
  23. clientContext.executeQueryAsync(Function.createDelegate(this,this.Succeeded),Function.createDelegatethis,this.Failed));
  24. }
  25.  
  26.  
  27. function Succeeded() {
  28. alert("Succeeded");
  29. }
  30. function Failed(sender,args) {
  31. alert("fail");
  32. }
  33.  
  34.  
  35.  
  36.  
  37. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.