CRUD Operations on SharePoint List using JavaScript Object Model(JSOM)


JavaScript Object Model
Create Item
Add one record into ProductsCategory list using below code.

//Get the Site var context = new SP.ClientContex("Your Site URL"); // Get the Web var web = context.get_web(); // Get the list based on the Title var list = web.get_lists().getByTitle("ProductCategory"); //Object for creating Item in the List var listCreationInformation = new SP.ListItemCreationInformation(); var listItem = list.addItem(listCreationInformation); listItem.set_item("Title", $("#CategoryId").val()); listItem.set_item("CategoryName", $("#CategoryName").val()); listItem.update(); //Update the List Item ctx.load(listItem); //Execute the batch Asynchronously ctx.executeQueryAsync( Function.createDelegate(this, success), Function.createDelegate(this, fail) );



The above code perform the following operations.
  •  To add a new item in the list, the SP.ListCreationInformation() object is used
  •  This object is then passed to the addItem() method of the List. This method returns the ListItem object
  •  Using the set_item() method of the ListItem the values for each field in the List is set and finally the list is updated.
Update Item 
Edit the record from StudentsInfo list whose ID=1 using below code. 

/Get the site var contex = new SP.ClientContext("Your Site URL"); //Get the web var web = contex.get_web(); //Get the List based upon the Title var list = web.get_lists().getByTitle("StudentsInfo"); ctx.load(list); listItem = list.getItemById(1); ctx.load(listItem); listItem.set_item("StudentName", "Gopi Avala"); listItem.update(); ctx.executeQueryAsync(Function.createDelegate(this, success), Function.createDelegate(this, fail));


Delete Item 

Delete the record from StudentsInfo list whose ID=1 using below code.

//Get the site var contex = new SP.ClientContext("Your Site URL"); //Get the web var web = contex.get_web(); //Get the List based upon the Title var list = web.get_lists().getByTitle("StudentsInfo"); ctx.load(list); listItem = list.getItemById(1); ctx.load(listItem); listItem.deleteObject(); ctx.executeQueryAsync(Function.createDelegate(this, success), Function.createDelegate(this, fail));


Get all Item 
Get all the item from StudentsInfo list using below code  

//Get the site var contex = new SP.ClientContext("Your Site URL"); //Get the web var web = contex.get_web(); //Get the List based upon the Title var list = web.get_lists().getByTitle("StudentsInfo"); //The Query object. This is used to query for data in the List var query = new SP.CamlQuery(); ctx.load(list); query.set_viewXml('<View></View>'); var items = list.getItems(query); //Retrieves the properties of a client object from the server. ctx.load(list); ctx.load(items); ctx.executeQueryAsync( Function.createDelegate(this, function () { var enumerator = items.getEnumerator(); while (enumerator.moveNext()) { var currentListItem = enumerator.get_current(); alert(currentListItem.get_item("ID")); alert(currentListItem.get_item("Title")); alert(currentListItem.get_item("StudentName)); } }), Function.createDelegate(this, fail) );


Get Specific Item
//Get the site var contex = new SP.ClientContext("Your Site URL"); //Get the web var web = contex.get_web(); //Get the List based upon the Title var list = web.get_lists().getByTitle("StudentsInfo"); //The Query object. This is used to query for data in the List var query = new SP.CamlQuery(); ctx.load(list); //Create the CAML that will return only items with the titles that begin with 'G' query.set_viewXml('<View><Query><Where><BeginsWith><FieldRef Name="StudentName" /><Value Type="Text">G</Value></BeginsWith></Where></Query></View>'); var items = list.getItems(query); //Retrieves the properties of a client object from the server. ctx.load(list); ctx.load(items); ctx.executeQueryAsync( Function.createDelegate(this, function () { //Get an enumerator for the items in the list var enumerator = items.getEnumerator(); while (enumerator.moveNext()) { var currentListItem = enumerator.get_current(); alert(currentListItem.get_item("ID")); alert(currentListItem.get_item("Title")); alert(currentListItem.get_item("StudentName")); } }), Function.createDelegate(this, fail) );

Happy Coding....Sharing is caring .... Keep Smiling...

Comments

Popular posts from this blog

SharePoint 2013 Keyword Query (KQL) Content Class Property Restrictions

SharePoint SPFx Intro & Configuration to Create New SPFx Solution

Filtering the Sharepoint List Taxonomy Column using Rest API