The following is the full code to add, update and delete SharePoint list item in C#
In this post, I use the list named "Employee" in the previous posts
Add a new item
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["Employee"];
SPListItem listItem = list.Items.Add();
listItem["Title"] = "Betty";
listItem["Birthday"] = "1/1/1990";
listItem["Male"] = 1;
listItem["Position"] = "Tester";
listItem.Update();
Edit an item
for (int i = 0; i < itemCol.Count; i++)
{
SPListItem item = itemCol[i];
if(item["ID"].ToString() == "2") // Find an item by ID then updating it
{
item["Title"] = "Linda";
item["Birthday"] = "1/2/1990";
item["Male"] = 0; //the Yes/No field type, 0 refers to female and 1 refers to male
item["Position"] = "Tester";
item.Update();
break;
}
}
Delete an item
list.Items.DeleteItemById(7);
Hope this helps!
Tuesday, December 27, 2011
Programmatically adding, editing, deleting SharePoint List Items
8:47 PM
Unknown
No comments
0 comments:
Post a Comment