SharePoint 2010 – List – Add, Edit, Delete using code

In this article we can experiment List manipulations through code. Following are the list operations to perform:

1. Add

2. Edit

3. Delete

Pre-Requisite

For proceeding with we need to create a List named Tasks using template Tasks.

clip_image002

Now create a new SharePoint Console Application project inside Visual Studio.

clip_image004

Make sure you changed the Application Target Framework to .Net 3.5 version.

Adding an Item

For adding a new Task Item execute the following code:

using (SPSite site = new SPSite("http://appes-pc"))

{

using (SPWeb web = site.OpenWeb())

{

SPList list = web.Lists["Tasks"];

SPListItem item = list.Items.Add();

item["Title"] = "New Task";

item["Description"] = "Description of Task";

item.Update();

}

}

Now you can check the Tasks list inside SharePoint and you can see the new item there.

clip_image006

Editing an Item

For editing an existing Task use the following code. Here we are changing the first item Title and Description.

using (SPSite site = new SPSite("http://appes-pc"))

{

using (SPWeb web = site.OpenWeb())

{

SPList list = web.Lists["Tasks"];

SPListItem item = list.Items[0];

item["Title"] = "Edited Task";

item["Description"] = "Description of Task (edited)";

item.Update();

}

}

Going back to SharePoint you can see the Edited Task

clip_image008

Deleting an Item

For deleting an item use the following code.

using (SPSite site = new SPSite("http://appes-pc"))

{

using (SPWeb web = site.OpenWeb())

{

SPList list = web.Lists["Tasks"];

SPListItem item = list.Items[0];

item.Delete();

}

}

Now you can go back to SharePoint and see that the item was deleted.

clip_image010

References

http://suguk.org/blogs/tomp/archive/2006/09/28/Adding_SPListItems.aspx

Summary

In this article we have experimented List manipulations Add, Edit, Delete through code. In real life we will be needed to automate List manipulations and programming will be a help in this regard.

2 thoughts on “SharePoint 2010 – List – Add, Edit, Delete using code

  1. I personally was basically looking for recommendations for my blog and located
    your blog, “SharePoint 2010 – List – Add, Edit, Delete using code « Jean Paul’s Blog”, will you mind if I personally utilize a few of your own tips? Thanks ,Margarita

    1. Jean Paul says:

      Sure! Go ahead my dear friend.

Leave a Reply

Your email address will not be published. Required fields are marked *