CRUD Operations on SharePoint List using Client Side Object Model(CSOM)


Client Side Object Model
Create Item
Add one record into StudentsInfo list using below code.


/Get the site string siteUrl = "SiteURL"; ClientContext clientContext = new ClientContext(siteUrl); // Get the List List oList = clientContext.Web.Lists.GetByTitle("StudentsInfo"); ListItemCreationInformation listCreationInformation = new ListItemCreationInformation(); ListItem oListItem = oList.AddItem(listCreationInformation); oListItem["Title"] = "Mr"; oListItem["StudentName"] = "Gopi Avala"; oListItem.Update(); clientContext.ExecuteQuery();



Update Item 
Edit the record from StudentsInfo list whose ID=1 using below code. 

//Get the site string siteUrl = "SiteURL"; ClientContext clientContext = new ClientContext(siteUrl); // Get the List List oList = clientContext.Web.Lists.GetByTitle("StudentsInfo"); ListItem oListItem = oList.GetItemById(1); oListItem["Title"] = "Gopi"; oListItem.Update(); clientContext.ExecuteQuery();


Delete Item

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

//Get the site string siteUrl = "SiteURL”; ClientContext clientContext = new ClientContext(siteUrl); // Get the List List oList = clientContext.Web.Lists.GetByTitle("StudentsInfo"); //Pass your ID ListItem oListItem = oList.GetItemById(1); oListItem.DeleteObject(); clientContext.ExecuteQuery();


Get all Items from List
Get all the items from StudentsInfo list using below code 
//Get the site string siteUrl = "SiteURL"; ClientContext clientContext = new ClientContext(siteUrl); // Get the List List oList = clientContext.Web.Lists.GetByTitle("StudentsInfo"); CamlQuery query = new CamlQuery(); query.ViewXml = "<View/>"; ListItemCollection items = oList.GetItems(query); clientContext.Load(oList); clientContext.Load(items); clientContext.ExecuteQuery();


Get Specific Item
Get all matching record from employee list where StudentName='Gopi Avala' using below code  

/Get the site string siteUrl = "SiteURL"; ClientContext clientContext = new ClientContext(siteUrl); // Get the List List oList = clientContext.Web.Lists.GetByTitle("StudentsInfo"); CamlQuery query = new CamlQuery(); query.ViewXml = @ "<View><Query><Where><Eq><FieldRef Name = 'StudentName'/><Value Type='Text'>Gopi Avala</Value> </Eq></Where></Query></View>"; ListItemCollection listItems = oList.GetItems(query); clientContext.Load(listItems, items => items.Include( item => item["Id"], item => item["Title"], item => item["StudentName"] )); clientContext.ExecuteQuery();

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

Comments

Popular posts from this blog

SharePoint 2013 Keyword Query (KQL) Content Class Property Restrictions

Filtering the Sharepoint List Taxonomy Column using Rest API

SharePoint CSOM to Create Folders and Sub Folders based on Excel Data