SharePoint 2010 Client side and server side code for CRUD list item. Sample contains client side object model
Client Object Model | LINQ to SharePoint | |
Create List Items | string siteUrl = "http://cmsstores"; ClientContext clientContext = new ClientContext(siteUrl); SP.List oList = clientContext.Web.Lists.GetByTitle("Students"); ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation(); ListItem oListItem = oList.AddItem(itemCreateInfo); oListItem["Firstname"] = txtFname.Text.Trim(); oListItem["Lastname"] = txtLname.Text.Trim(); oListItem.Update(); clientContext.ExecuteQuery(); |
try { using (MyEntitiesDataContext dc = new MyEntitiesDataContext(SPContext.Current.Site.Url)) { EntityList listItems = dc.GetList("Students"); StudentsItem objnew = new StudentsItem() { Firstname = txtFname.Text.Trim(), Lastname = txtLname.Text.Trim(), Country = Country.USA, Title = txtTitle.Text.Trim() };// Insert the new list item to the list dc.Students.InsertOnSubmit(objnew); dc.SubmitChanges(); } } catch (Exception ex) { throw; } |
Update List Items | string siteUrl = "http://cmsstores"; ClientContext = new ClientContext(siteUrl); SP.List oList = clientContext.Web.Lists.GetByTitle("Students"); ListItem oListItem = oList.GetItemById(6); oListItem["Firstname"] = txtFname.Text.Trim(); oListItem["Lastname"] = txtLname.Text.Trim(); oListItem.Update(); clientContext.ExecuteQuery(); |
try { using (MyEntitiesDataContext dc = new MyEntitiesDataContext(SPContext.Current.Site.Url)) { EntityList listItems = dc.GetList("Students"); StudentsItem updateobj = (from c in listItems where c.Id == 4 select c).First(); updateobj.Lastname = "Item2"; updateobj.Firstname = "Item2"; txtLname.Text = updateobj.Id.ToString(); dc.Students.InsertOnSubmit(updateobj); dc.SubmitChanges(); } } catch (Exception ex) { } |
Delete List Item | string siteUrl = "http://cmsstores"; ClientContext = new ClientContext(siteUrl); SP.List oList = clientContext.Web.Lists.GetByTitle("Students"); ListItem oListItem = oList.GetItemById(6); oListItem.DeleteObject(); clientContext.ExecuteQuery(); |
try { using (MyEntitiesDataContext dc = new MyEntitiesDataContext(SPContext.Current.Site.Url)) { EntityList listItems = dc.GetList("Students"); var updateobj = (from c in listItems where c.Firstname == objTitle select c).First(); dc.Students.DeleteOnSubmit(updateobj); //Submit the changes dc.SubmitChanges(); } } catch (Exception ex) { } |
Similar Topics:
- Sharepoint OM, getting email ID from login user name
- Microsoft Sharepoint updating Author-Created By or Editor field programmatically
- Microsoft Sharepoint SystemUpdate and Update using object model
- List enhancements in SharePoint 2010
- SharePoint Developer Interview Questions and Answers
- Sharepoint best practices advantages of storing scripts in layout folder
- Developer favorites SharePoint 2010 enhancements
- SPDisposeCheck – Sharepoint Tools to check memory leaks
- Sharepoint 2010 Quick learn video Tutorial 1
- More about SharePoint Custom page and Application Page