1 out of 8 rated this helpful Rate this topic

How to: Add or Delete List Items

Published: May 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.

NoteNote

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.

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.

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.

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

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.

Date

Description

Reason

May 2010

Initial publication

Did you find this helpful?
(2000 characters remaining)
Community Content Add
Annotations FAQ
Delete does not delete all items - Solution
This code does not delete all items in the list. It leaves one item in the end. Looking at other comments it seems it worked for some people. Each person has a different environment, different hardware and different software. For me, this code does not delete all items. If you delete items using indexes, remember, after deleting one item, the indexes change. Using index 0 instead of dynamic index should resolve the issue but it doesn't. It leaves the last item in the collection as it is. The best solution for this sample is to delete the items using unique identifier. Here is how you do it. I am not saying this is the best practice. It is not! There are different ways to delete items from a list. I am just giving a solution for the apparent problem in the sample code.


SPWeb mySite = SPContext.Current.Web;
SPListItemCollection listItems = mySite.Lists[TextBox1.Text].Items;
int itemCount = listItems.Count;

Guid[] itemArray;
itemArray = new Guid[itemCount];

foreach(SPListItem item in listItems)
{
 itemArray[i] = item.UniqueId;
 i = i + 1;
}

for (int k=0; k<itemCount; k++)
{
    SPListItem item = listItems[itemArray[k]];
    if (TextBox2.Text==item["Employee"].ToString())
    {
        listItems[itemArray[k]].Delete();
    }
}

Again, this is NOT an efficient way of handling the delete task but at least your sample code will run.
Bad way to add item
$0It's bad way to add item.$0 $0Becouse create empty item before adding data very bad!$0
Likely Performs Very Badly
Unless things have changed in 2010, you don't really ever want to perform the call m_spListObject.Items because that loads all the items from the DB.

A better way to create a new SPListItem is to perform an empty SPQuery on the list thus returning a list item with the specified schema. Filll it in and then Save it.
Delete List Items
In Delete List Items code don't forget to add given below line after delete item line (listItems.Delete(k);). Other wise you will face exception as "Specified argument was out of the range of valid values".
itemCount = itemCount -1;