CRUD operations in SharePoint hosted apps using Javascript Object Model (JSOM)

Posted on Updated on

SharePoint hosted apps can be developed using only Javascript, but still we have 2 options and they’re Javascript Object Model (JSOM) for the SharePoint and SharePoint client REST API. This post explains how to perform CRUD operations in a list using JSOM. Click this link to see how to perform CRUD operations using REST API and Javascript.

The following code is written in ‘Revealing Module Pattern’ using the self invoking function.  A simple built it Contact List is used in this sample.

   1: var Qbe = window.Qbe || {};

   2: Qbe.Contacts;

   3: Qbe.ContactCRUD = Qbe.ContactCRUD || {};

   4:  

   5: Qbe.ContactCRUD = function () {

   6:  

   7:  

   8:     createItem = function (fname, lname, phone) {

   9:         var ctx = new SP.ClientContext.get_current();

  10:         var list = ctx.get_web().get_lists().getByTitle('ContactList');

  11:         ctx.load(list);

  12:  

  13:         var listItemCreationInfo = new SP.ListItemCreationInformation();

  14:  

  15:         var newContact = list.addItem(listItemCreationInfo);

  16:         newContact.set_item('Title', lname);

  17:         newContact.set_item('FirstName', fname);

  18:         newContact.set_item('WorkPhone', phone);

  19:  

  20:         newContact.update();

  21:         ctx.executeQueryAsync(success, error);

  22:     },

  23:  

  24:     readAll = function () {

  25:         var ctx = new SP.ClientContext.get_current();

  26:         var query = "<View><Query><OrderBy><FieldRef Name='Title'/>" +

  27:         "<FieldRef Name='FirstName'/></OrderBy></Query>" +

  28:         "<ViewFields><FieldRef Name='ID'/><FieldRef Name='Title'/>" +

  29:         "<FieldRef Name='FirstName'/><FieldRef Name='WorkPhone'/></ViewFields></View>";

  30:  

  31:         var camlQuery = new SP.CamlQuery();

  32:         camlQuery.set_viewXml(query);

  33:  

  34:         var list = ctx.get_web().get_lists().getByTitle('ContactList');

  35:         ctx.load(list);

  36:  

  37:         Qbe.Contacts = list.getItems(camlQuery);

  38:         ctx.load(Qbe.Contacts, 'Include(ID,Title,FirstName,WorkPhone)');

  39:  

  40:         ctx.executeQueryAsync(readAllSuccess, error);

  41:     },

  42:  

  43:     readAllSuccess = function () {

  44:         var html = [];

  45:         html.push("<table><thead><tr><th>ID</th><th>First Name</th>");

  46:         html.push("<th>Last Name</th><th>Title</th></tr></thead>");

  47:  

  48:         var listItemEnumerator = Qbe.Contacts.getEnumerator();

  49:  

  50:         while (listItemEnumerator.moveNext()) {

  51:             var listItem = listItemEnumerator.get_current();

  52:             html.push("<tr><td>");

  53:             html.push(listItem.get_item("ID"));

  54:             html.push("</td><td>");

  55:             html.push(listItem.get_item("FirstName"));

  56:             html.push("</td><td>");

  57:             html.push(listItem.get_item("Title"));

  58:             html.push("</td><td>");

  59:             html.push(listItem.get_item("WorkPhone"));

  60:             html.push("</td><td>");

  61:         }

  62:         html.push("</table>");

  63:         $('#displayDiv').html(html.join(''));

  64:     },

  65:  

  66:     updateItem = function (id, fname, lname, phone) {

  67:         var ctx = new SP.ClientContext.get_current();

  68:         var list = ctx.get_web().get_lists().getByTitle('ContactList');

  69:         ctx.load(list);

  70:  

  71:         var item = list.getItemById(id);

  72:         item.set_item('Title', lname);

  73:         item.set_item('FirstName', lname);

  74:         item.set_item('WorkPhone', phone);

  75:  

  76:         item.update();

  77:         ctx.executeQueryAsync(success, error);

  78:     },

  79:  

  80:     deleteItem = function (id) {

  81:         var ctx = new SP.ClientContext.get_current();

  82:         var list = ctx.get_web().get_lists().getByTitle('ContactList');

  83:         ctx.load(list);

  84:  

  85:         var item = list.getItemById(id);

  86:         item.deleteObject();

  87:         

  88:         ctx.executeQueryAsync(success, error);

  89:     },

  90:  

  91:     success = function () {

  92:         readAll();

  93:     },

  94:     error = function (sender, args) {

  95:         alert(args.get_message());

  96:     }

  97:  

  98:     // public interface

  99:     return {

 100:         createContact: createItem,

 101:         updateContact: updateItem,

 102:         deleteContact: deleteItem

 103:     }

 104: }();

Advertisements