SharePoint 2013 - SPLessons

SharePoint List Items Adding/Updating/Deleting

Home > Lesson > Chapter 28
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

SharePoint List Items Adding/Updating/Deleting

SharePoint List Items Adding/Updating/Deleting

 

Adding  the items Into the Sharepoint list

[csharp]using System; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using System.Collections; using USL.AppPages.Layouts.USL.AppPages; using System.Data; namespace ServerObjectModelExample { class Program { static void Main(string[] args) { using (SPSite oSite = new SPSite(SPContext.Current.Web.Url)) { using (SPWeb objWeb = oSite.OpenWeb()) { SPUser currentuser = objWeb.CurrentUser; SPList lstPOCreation = objWeb.Lists["Splessons"]; SPListItem lstItem; lstItem = lstPOCreation.AddItem(); lstItem["Title"] = "SreeHari"; lstItem["Location"] = "India"; objWeb.AllowUnsafeUpdates = true; lstItem.Update(); objWeb.AllowUnsafeUpdates = false; } } } } }[/csharp]

Updating the existing  items Into the Sharepoint list

Updating an existing item from a SharePoint List is very similar to adding a new item, but the only difference is instead of calling Add() from the SPListItemCollection, I now call the GetItemById(int) from the SPList object. There are other ways of getting an item from a list and another one is GetItemByTitle. [csharp]using System; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using System.Collections; using USL.AppPages.Layouts.USL.AppPages; using System.Data; namespace ServerObjectModelExample { class Program { static void Main(string[] args) { using (SPSite oSite = new SPSite(SPContext.Current.Web.Url)) { using (SPWeb objWeb = oSite.OpenWeb()) { SPUser currentuser = objWeb.CurrentUser; SPList lstPOCreation = objWeb.Lists["Splessons"]; SPListItem lstItem; SPListItem lstItem = spList.GetItemById(1); lstItem["Title"] = " Inukollu SreeHari"; lstItem["Location"] = "Texas"; objWeb.AllowUnsafeUpdates = true; lstItem.Update(); objWeb.AllowUnsafeUpdates = false; } } } } }[/csharp]

Deleting the existing  items Into the Sharepoint list

Deleting an existing item from a SharePoint List all I need to do is get the item I want from this list and then call the Delete method from the item object. [csharp]using System; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using System.Collections; using USL.AppPages.Layouts.USL.AppPages; using System.Data; namespace ServerObjectModelExample { class Program { static void Main(string[] args) { using (SPSite oSite = new SPSite(SPContext.Current.Web.Url)) { using (SPWeb objWeb = oSite.OpenWeb()) { SPUser currentuser = objWeb.CurrentUser; SPList lstPOCreation = objWeb.Lists["Splessons"]; SPListItem lstItem; SPListItem lstItem = spList.GetItemById(1); objWeb.AllowUnsafeUpdates = true; lstItem.Delete(); objWeb.AllowUnsafeUpdates = false; } } } } }[/csharp]