How to: Add or Delete List Items

Applies to: SharePoint Foundation 2010

The examples in this topic show how to use the SharePoint Foundation server object model to create or delete list items in a Web site or site collection.

Note

The code examples in this topic use members of the Microsoft.SharePoint.SPContext class to obtain the current site collection, Web site, or list. Outside an HTTP context, such as in a console application or a Windows application, you obtain references to key objects with a different method. Instead of using an Microsoft.SharePoint.SPContext object, use the SPSite() constructor to instantiate a specific site collection and obtain objects. For more information, see Getting References to Sites, Web Applications, and Other Key Objects.

Creating List Items

To add items to a list, use the Add method of the SPListItemCollection class to create an item object, and then use the Update method of the SPListItem class to update the database with the new item.

The following example assumes the existence of five text boxes, one that specifies the name of the list to add items to, and four other text boxes that are used to specify the values to add. Indexers gather input from all five sources. The example also assumes that the list specified by TextBox1.Text exists.

Dim mySite As SPWeb = SPContext.Current.Web
Dim listItems As SPListItemCollection 
    = mySite.Lists(TextBox1.Text).Items

Dim item As SPListItem = listItems.Add()

item("Title") = TextBox2.Text
item("Stock") = Convert.ToInt32(TextBox3.Text)
item("Return Date") = Convert.ToDateTime(TextBox4.Text)
item("Employee") = TextBox5.Text

item.Update()
SPWeb mySite = SPContext.Current.Web;
SPListItemCollection listItems = mySite.Lists[TextBox1.Text].Items;

SPListItem item = listItems.Add();

item["Title"] = TextBox2.Text;
item["Stock"] = Convert.ToInt32(TextBox3.Text);
item["Return Date"] = Convert.ToDateTime(TextBox4.Text);
item["Employee"] = TextBox5.Text;

item.Update();
}

The example first creates an SPListItem object by using the Add method of the collection. It then assigns values to specific fields by using an indexer on the list item. For example, item["Title"] specifies the Title column value for the item. Finally, the example calls the Update method of the list item to effect changes in the database. You can also use the Author, Editor, Created, and Modified fields as indexers, where Author or Editor specify a Microsoft SharePoint Foundation user ID. For an example, see the SPListItem class.

Deleting List Items

To delete items from a list, use the Delete method of the SPListItemCollection class, which takes an index into the collection as its parameter.

Dim mySite As SPWeb = SPContext.Current.Web
Dim listItems As SPListItemCollection 
    = mySite.Lists(TextBox1.Text).Items
Dim itemCount As Integer = listItems.Count
Dim k As Integer

For k = 0 To itemCount - 1
    Dim item As SPListItem = listItems(k)

    If TextBox2.Text = item("Employee").ToString() Then
        listItems.Delete(k)
    End If
Next k
SPWeb mySite = SPContext.Current.Web;
SPListItemCollection listItems = mySite.Lists[TextBox1.Text].Items;
int itemCount = listItems.Count;

for (int k=0; k<itemCount; k++)
{
    SPListItem item = listItems[k];

    if (TextBox2.Text==item["Employee"].ToString())
    {
        listItems.Delete(k);
    }
}

Based on input from two text boxes, the example iterates through the collection of items for the specified list and deletes an item if the Employee field value matches the specified value.

The previous examples require a using directive (Imports in Visual Basic) for the Microsoft.SharePoint namespace.

See Also

Reference

Microsoft.SharePoint

Concepts

Working with List Objects and Collections

Using Visual Studio for SharePoint Development

Security Validation and Making Posts to Update Data

Elevation of Privilege