SIGN UP MEMBER LOGIN:    
ARTICLE

SharePoint 2010 - List - Add, Edit, Delete Using Code

Posted by Jean Paul Articles | SharePoint February 07, 2012
In this article we will experiment with List manipulations through code.
Reader Level:

In this article we will experiment with List manipulations through code. The following are the list operations to perform:

  1. Add
  2. Edit
  3. Delete

Pre-Requisite

Before proceeding we need to create a List named Tasks using the Tasks template.

Share1.gif

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

Share2.gif

Make sure you changed the Application Target Framework to the .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 will see the new item there.

Share3.gif

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:

Share4.gif

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.

Share5.gif

References

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

Summary

In this article we have experimented with the List manipulations Add, Edit and Delete through code. In real life we need to automate List manipulations and programming will help with that.
 

Login to add your contents and source code to this article
share this article :
post comment
 

Thank You Sumita and Yes I will add more SP articles for sure.

Posted by Jean Paul Feb 09, 2012

nice article...keep posting more...

Posted by Sumita Jain Feb 08, 2012

Thank You for the good words Sonakshi.. I am working on more SP 2010 articles.

Posted by Jean Paul Feb 08, 2012

Thank You Adora... Great to see your appreciations.

Posted by Jean Paul Feb 08, 2012

Thank You Arjun.. That was my first attempt in SharePoint 2010

Posted by Jean Paul Feb 08, 2012
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor