This topic has not yet been rated - Rate this topic

SPListItemCollection.Delete Method

Deletes the item at the specified index in the collection.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
public void Delete(
	int iIndex
)

Parameters

iIndex
Type: System.Int32
A 32-bit integer that specifies the index.

The Delete method deletes an item based on its index in the collection. To delete an item based on its ID, use the DeleteItemById method.

This method returns an ArgumentOutOfRangeException exception if the specified index is outside the valid range of indices for the collection.

The following code example deletes any items from the specified list in which an integer field value is less than 70 or a text field value equals None.

The For loop in the example counts downward (intindex-- ) instead of upward (intindex++ ) because items are being deleted and the number of items decreases with each increment.

SPWeb oWebsite = SPContext.Current.Web;
SPList oList = oWebsite.Lists["List_Name"];
SPListItemCollection collListItems = oList.Items;

for (int intIndex = collListItems.Count - 1; intIndex > -1; intIndex--)
{
    if (Convert.ToInt32(collListItems[intIndex]["Field1_Name"]) < 70 ||
        collListItems[intIndex]["Field2_Name"].ToString() == "None")
    {
        collListItems.Delete(intIndex);
    }
}

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ