CRUD Operation In SharePoint Using JavaScript

CRUD operations using JavaScript are common programs for anyone who is a beginner, intermediate or expert level. During CRUD operations, the programmer is facing different types of errors and it will take lot of time to resolve.

This article shows how to insert, update and delete the records in SharePoint List, using client side code.

SharePoint 2013 provides a JavaScript Object Model to interact with SharePoint list or library by use of .js files. Here, we should use any editor to write the code.
 
Once we finish the code, we have to use it as inline inside a SharePoint site. Please look into the code given below for adding an item in a list, using JavaScript Object Model.

To create a list item, you should always use ListItemCreationInformation object.
  1. <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>    
  2. <script type="text/javascript" src="/_layouts/15/sp.core.js"></script>    
  3. <script type="text/javascript" src="/_layouts/15/sp.js"></script>    
  4.      
  5. <script type="text/javascript">    
  6. var clientContext;    
  7. var oListItem;    
  8. var itemId;    
  9. var oList;    
  10.      
  11. function createListItem() {    
  12. clientContext = new SP.ClientContext.get_current();    
  13. oList = clientContext.get_web().get_lists().getByTitle('Emp_Details');    
  14.      
  15. var itemCreateInfo = new SP.ListItemCreationInformation();    
  16. this.oListItem = oList.addItem(itemCreateInfo);    
  17.      
  18. var FirstName = "Raj";    
  19. var LastName = "Kiran";    
  20.      
  21. oListItem.update();    
  22.      
  23. clientContext.load(oListItem);    
  24.      
  25. clientContext.executeQueryAsync(    
  26. Function.createDelegate(thisthis.onQueryInsertSucceeded),    
  27. Function.createDelegate(thisthis.onQueryInsertFailed));    
  28. }    
  29.      
  30. function onQueryInsertSucceeded() {    
  31. alert('Item Inserted: ');    
  32. }    
  33.      
  34. function onQueryInsertFailed() {    
  35. alert("Item Insert Failed");    
  36. }    
  37. </script>                                          
Updating  item in a list using Javascript object model

To update the item in a list, you should always use an updatet() function of the object. getItemId(1) means , it will delete the item, where id=1.
  1. <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>  
  2. <script type="text/javascript" src="/_layouts/15/sp.core.js"></script>  
  3. <script type="text/javascript" src="/_layouts/15/sp.js"></script>  
  4.    
  5. <script type="text/javascript">  
  6. var clientContext;  
  7. var oListItem;  
  8. var itemId;  
  9. var oList;  
  10.    
  11. function createListItem() {  
  12. clientContext = new SP.ClientContext.get_current();  
  13. oList = clientContext.get_web().get_lists().getByTitle('Emp_Details');  
  14.    
  15. this.oListItem =oList.getItemById(1);  
  16.   
  17. oListItem.set_item('Title''My Updated New Title');  
  18.   
  19. oListItem.update();  
  20.    
  21.    
  22. clientContext.load(oListItem);  
  23.    
  24. clientContext.executeQueryAsync(  
  25. Function.createDelegate(thisthis.onQueryInsertSucceeded),  
  26. Function.createDelegate(thisthis.onQueryInsertFailed));  
  27. }  
  28.    
  29. function onQueryInsertSucceeded() {  
  30. alert('Item Updated: ');  
  31. }  
  32.    
  33. function onQueryInsertFailed() {  
  34. alert("Item Update Failed");  
  35. }  
  36. </script>  
Deleting an item in a list using JavaScript Object Model

To delete the item in a list, you should always use deleteobject() function of the object. getItemId(1) means , it will delete the item, where id=1. 
  1. <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>  
  2. <script type="text/javascript" src="/_layouts/15/sp.core.js"></script>  
  3. <script type="text/javascript" src="/_layouts/15/sp.js"></script>  
  4.    
  5. <script type="text/javascript">  
  6. var clientContext;  
  7. var oListItem;  
  8. var itemId;  
  9. var oList;  
  10.    
  11. function createListItem() {  
  12. clientContext = new SP.ClientContext.get_current();  
  13. oList = clientContext.get_web().get_lists().getByTitle('Emp_Details');  
  14.    
  15. this.oListItem =oList.getItemById(1);  
  16.   
  17. oListItem.deleteObject();  
  18.    
  19.    
  20. clientContext.load(oListItem);  
  21.    
  22. clientContext.executeQueryAsync(  
  23. Function.createDelegate(thisthis.onQueryInsertSucceeded),  
  24. Function.createDelegate(thisthis.onQueryInsertFailed));  
  25. }  
  26.    
  27. function onQueryInsertSucceeded() {  
  28. alert('Item Deleted: ');  
  29. }  
  30.    
  31. function onQueryInsertFailed() {  
  32. alert("Item Deleted Failed");  
  33. }  
  34. </script>   
Once everything is completed, open your target site.
  1. Create a page.
  2. Edit the page.
  3. Add Webparts-> Add Script Editor Webpart
  4. Add your code inside Script Editor webpart.
  5. Now, click OK. 
Thank you for reading my blog.
Next Recommended Reading CRUD Operation In SharePoint 2013