In this blog, we will perform Create, Update and Delete on SharePoint list items using C# code. You may be interested in reading some of the important blogs – CAML Query in SharePoint, SharePoint 2013 Tutorial, SharePoint 2013 important interview questions.
What is SharePoint List?
SharePoint List is similar to a table in SQL database and contains data in rows and columns.If you are a SharePoint developer, you must have worked with SharePoint list either through Out of Box or with customization.
Working with SharePoint List through Out of Box is very easy but in some cases, if you want to manipulate SharePoint List items using C# code.
Below is the Add, Edit, Delete operation on SharePoint List using C# code
Add item to SharePoint List
Below C# code snippet will add new item to existing SharePoint List.
public class WebPartDemo : WebPart
{
protected override void CreateChildControls()
{
SPWeb mySite = SPContext.Current.Web;
SPListItemCollection listItems = mySite.Lists["Employee"].Items;
SPListItem item = listItems.Add();
item["Title"] = "New title";
item["Name"] = "SharePoint";
item.Update();
} }
Or you can write above code in below style as well.
using (SPSite site = new SPSite("siteurl"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Employee"];
SPListItem item = list.Items.Add();
item["Title"] = "New Title 2";
item["Name"] = "SharePoint Cafe";
web.AllowUnsafeUpdates = true;
item.Update();
} }
Update Item in SharePoint List
Below C# code will update an existing item in a SharePoint List.
using (SPSite site = new SPSite("siteurl"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Employee"];
SPListItem item = list.Items[0];
item["Title"] = "Edited Task";
item["Name"] = "Description of Task (edited)";
web.AllowUnsafeUpdates = true;
item.Update();
} }
Delete an item from SharePoint List
To delete an existing item, use below code.
using (SPSite site = new SPSite("siteurl"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Employee"];
SPListItem item = list.Items[0];
web.AllowUnsafeUpdates = true;
item.Delete();
} }
OR you can write this -
SPWeb mySite = SPContext.Current.Web;
SPListItemCollection listItems = mySite.Lists["Employee"].Items;
int itemCount = listItems.Count;
for (int k = 0; k < itemCount; k++)
{
SPListItem item = listItems[k];
if (item["Name"].ToString() == "SharePoint")
{
mySite.AllowUnsafeUpdates = true;
listItems.Delete(k);
} }
Delete a SharePoint List
You may use below code snippet to delete a SharePoint List.
SPWeb mySite = SPContext.Current.Web;
SPListCollection lists = mySite.Lists;
SPList list = lists["Store"];
System.Guid listGuid = list.ID;
mySite.AllowUnsafeUpdates = true;
lists.Delete(listGuid);
OR you can write like this -
SPSite mySite = SPContext.Current.Site;
SPWebCollection allWebs = mySite.AllWebs;
foreach (SPWeb web in allWebs)
{ SPListCollection allLists = web.Lists;
for (int i = 0; i < allLists.Count; i++)
{ SPList list = allLists[i];
if (list.Title == "City")
{ Guid listGuid = list.ID;
web.AllowUnsafeUpdates = true;
allLists.Delete(listGuid);
}
}
}
Create a SharePoint List
Below C# code is to create a new SharePoint List.
string listTitle = "MylistName";
string listDescription = "This is my custom list";
SPSite mySite = SPContext.Current.Site;
SPWebCollection allWebs = mySite.AllWebs;
foreach (SPWeb web in allWebs)
{
SPListCollection allLists = web.Lists;
web.AllowUnsafeUpdates = true;
allLists.Add(listTitle, listDescription, SPListTemplateType.GenericList);
Hope you are now ready to manipulate SharePoint List using C# code.
Keep following SharePointCafe.Net