In this blog post I will explain and demonstrate how to Add, Update and Delete a SharePoint List Item using the server side object model.
Adding a New Item
In the code sample below I obtain the current context and the web and get the list I would like to add the new item to. After I have assigned values to my fields I call an update method from the item.
SPWeb spWeb = SPContext.Current.Web; SPList spList = spWeb.Lists["MyList"]; SPListItem item = spList.Items.Add(); item["Title"] = "Hello, world!"; item.Update();
Update an Existing Item
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.
SPWeb spWeb = SPContext.Current.Web; SPList spList = spWeb.Lists["MyList"]; SPListItem item = spList.GetItemById(1); item["Title"] = "Hello, world!"; item.Update();
Deleting an Existing Item
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. That’s how easy it is.
SPWeb spWeb = SPContext.Current.Web; SPList spList = spWeb.Lists["MyList"]; SPListItem item = spList.GetItemById(1); item.Delete();
I hope the information I have provided in this post is useful to you.
appreciate your effort