Share via


SharePoint 2013 : List Item CRUD Operation using JSOM (Part 2)

Introduction

The goal of this article is to provide how to perform basic create, read, update, and delete (CRUD) operations on lists and list items with the JSOM. We have explored the CRUD operation using Web Service in my previous article.

Now, we will demo all the operations on list items, including retrieve, create, update and delete on list items.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/sagarp/crud-operation-on-list-items-using-jsomin-sharepoint-2013/Images/SharePoint.jpg 

Retrieve the list items

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/sagarp/crud-operation-on-list-items-using-jsomin-sharepoint-2013/Images/addingItem.png

Here is the main code in detail:

  1. function retriveListItem() 
  2.  //Get the current context 
  3.  var context = new SP.ClientContext(); 
  4.  var list = context.get_web().get_lists().getByTitle(‘companyInfo’); 
  5.  var caml = new SP.CamlQuery(); 
  6.  caml.set_viewXml("<View><Query><OrderBy><FieldRef Name=’Company’ Ascending='TRUE' /></OrderBy></Query></View>"); 
  7.  returnedItems = list.getItems(caml); 
  8.  context.load(returnedItems); 
  9.  context.executeQueryAsync(onSucceededCallback, onFailedCallback); 
  10.  
  11. function onSucceededCallback(sender, args) 
  12.  var enumerator = returnedItems.getEnumerator(); 
  13.  //Formulate HTML from the list items 
  14.  var MainResult = 'Items in the Divisions list: <br><br>'; 
  15.  //Loop through all the items 
  16.  while (enumerator.moveNext()) 
  17.  { 
  18.  var listItem = enumerator.get_current(); 
  19.  var companyName = listItem.get_item(“Company "); 
  20.   var Industry = listItem.get_item(“Industry "); 
  21.    MainResult += MainResult + companyName + "-" + Industry + "\n"; 
  22.    } 
  23.    //Display the formulated HTML in the displayDiv element 
  24.   displayDiv.innerHTML = MainResult; 
  25.   } 
  26.   //This function fires when the query fails 
  27.  function onFailedCallback(sender, args) 
  28.  { 
  29.   //Formulate HTML to display details of the error 
  30.   var markup = '<p>The request failed: <br>'; 
  31.   markup += 'Message: ' + args.get_message() + '<br>'; 
  32.   //Display the details 
  33.   displayDiv.innerHTML = markup; 
  34.  } 
  35.  }

Create list item

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/sagarp/crud-operation-on-list-items-using-jsomin-sharepoint-2013/Images/Create%20list%20items.jpg

Here is the main code in detail:

  1. function AddListItem() 
  2.  var listTitle = "companyInfo"; 
  3.  //Get the current client context 
  4.  context = SP.ClientContext.get_current(); 
  5.  var airportList = context.get_web().get_lists().getByTitle(listTitle); 
  6.  //Create a new record 
  7.  var listItemCreationInformation = new SP.ListItemCreationInformation(); 
  8.  var listItem = airportList.addItem(listItemCreationInformation); 
  9.  //Set the values 
  10.  Var industryVal = $("#Industry").val(); 
  11.  var Company = $("#Company").val(); 
  12.  listItem.set_item('Industry', +industryVal); 
  13.  listItem.set_item('Company', +new item); 
  14.  listItem.update(); 
  15.  context.load(listItem); 
  16.  context.executeQueryAsync(AddListItemSucceeded, AddListItemFailed); 
  17.  
  18. function AddListItemSucceeded() 
  19.  retriveListItem(); 
  20.  
  21. function AddListItemFailed(sender, args) 
  22.  alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); 

Update list item

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/sagarp/crud-operation-on-list-items-using-jsomin-sharepoint-2013/Images/Update%20list%20items.jpg

Here is the main code in detail:

  1. function updateListItem() 
  2.  var ListName = "companyInfo"; 
  3.  var context = new SP.ClientContext.get_current(); // the current context is taken by default here 
  4.  //you can also create a particular site context as follows 
  5.  var lstObject = context.get_web().get_lists().getByTitle(ListName); 
  6.  this.lstObjectItem = lstObject.getItemById(1); 
  7.  
  8.  Var industryVal = $("#Industry").val(); 
  9.  var Company = $("#Company").val(); 
  10.  lstObjectItem.set_item('Industry', “+industryVal + ”); 
  11.  lstObjectItem.set_item('Company', ”+Company + ”); 
  12.  lstObjectItem.update(); 
  13.       context.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFailure));  
  14.  
  15. function onSuccess() 
  16.  retriveListItem(); 
  17.  
  18. function onFailure(sender, args) 
  19.  alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); 

Delete list item

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/sagarp/crud-operation-on-list-items-using-jsomin-sharepoint-2013/Images/Delete%20list%20items.jpg

Here is the main code in detail:

  1. function deleteListItem() 
  2.  var listTitle = "companyInfo"; 
  3.  //get the current client context 
  4.  context = SP.ClientContext.get_current(); 
  5.  var airportList = context.get_web().get_lists().getByTitle(listTitle); 
  6.  //get the list item to delete 
  7.  var listItem = airportList.getItemById(1); 
  8.  //delete the list item 
  9.  listItem.deleteObject(); 
  10.  context.executeQueryAsync(DeleteItemSucceeded, DeleteItemFailed); 
  11.  
  12. function DeleteItemSucceeded() 
  13.  retriveListItem(); 
  14.  
  15. function DeleteItemFailed(sender, args) 
  16.  alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); 

Summary

In this article we explored SharePoint JSOM for CRUD operations on list items level. Hope it will be helpful.