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. I have explored the CRUD operation using Web Service in my previous article.
Now, I will demo all the operations on list items, including retrieve, create, update and delete on list items.
Retrieve the list items
Here is the main code in detail:
- function retriveListItem()
- {
-
- var context = new SP.ClientContext();
- var list = context.get_web().get_lists().getByTitle(‘companyInfo’);
- var caml = new SP.CamlQuery();
- caml.set_viewXml("<View><Query><OrderBy><FieldRef Name=’Company’ Ascending='TRUE' /></OrderBy></Query></View>");
- returnedItems = list.getItems(caml);
- context.load(returnedItems);
- context.executeQueryAsync(onSucceededCallback, onFailedCallback);
- }
-
- function onSucceededCallback(sender, args)
- {
- var enumerator = returnedItems.getEnumerator();
-
- var MainResult = 'Items in the Divisions list: <br><br>';
-
- while (enumerator.moveNext())
- {
- var listItem = enumerator.get_current();
- var companyName = listItem.get_item(“Company ");
- var Industry = listItem.get_item(“Industry ");
- MainResult += MainResult + companyName + "-" + Industry + "\n";
- }
-
- displayDiv.innerHTML = MainResult;
- }
-
- function onFailedCallback(sender, args)
- {
-
- var markup = '<p>The request failed: <br>';
- markup += 'Message: ' + args.get_message() + '<br>';
-
- displayDiv.innerHTML = markup;
- }
- }
- }
Create list item Here is the main code in detail:
- function AddListItem()
- {
- var listTitle = "companyInfo";
-
- context = SP.ClientContext.get_current();
- var airportList = context.get_web().get_lists().getByTitle(listTitle);
-
- var listItemCreationInformation = new SP.ListItemCreationInformation();
- var listItem = airportList.addItem(listItemCreationInformation);
-
- Var industryVal = $("#Industry").val();
- var Company = $("#Company").val();
- listItem.set_item('Industry', +industryVal);
- listItem.set_item('Company', +new item);
- listItem.update();
- context.load(listItem);
- context.executeQueryAsync(AddListItemSucceeded, AddListItemFailed);
- }
-
- function AddListItemSucceeded()
- {
- retriveListItem();
- }
-
- function AddListItemFailed(sender, args)
- {
- alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
- }
Update list item Here is the main code in detail:
- function updateListItem()
- {
- var ListName = "companyInfo";
- var context = new SP.ClientContext.get_current();
-
- var lstObject = context.get_web().get_lists().getByTitle(ListName);
-
-
- Var industryVal = $("#Industry").val();
- var Company = $("#Company").val();
- lstObjectItem.set_item('Industry', “+industryVal + ”);
- lstObjectItem.set_item('Company', ”+Company + ”);
- lstObjectItem.update();
- context.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFailure));
- }
-
- function onSuccess()
- {
- retriveListItem();
- }
-
- function onFailure(sender, args)
- {
- alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
- }
Delete list item Here is the main code in detail:
- function deleteListItem()
- {
- var listTitle = "companyInfo";
-
- context = SP.ClientContext.get_current();
- var airportList = context.get_web().get_lists().getByTitle(listTitle);
-
- var listItem = airportList.getItemById(1);
-
- listItem.deleteObject();
- context.executeQueryAsync(DeleteItemSucceeded, DeleteItemFailed);
- }
-
- function DeleteItemSucceeded()
- {
- retriveListItem();
- }
-
- function DeleteItemFailed(sender, args)
- {
- 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.