Search code examples
sharepoint-2007

Delete multiple sharepoint list items programmatically according to some column value


  • I want to delete Document Library items through code.
  • Suppose Doc library has one column FolderId.
  • I want to delete all items with FolderId 33.

I am trying to delete it through code, but only the first occurence with FolderId 33 is deleted.

When I to try to delete the second occurrence, the following exception occurs:

"Microsoft.SharePoint.SPException: Item does not exist. It may have been deleted by another user. at Microsoft.SharePoint.SPListItem.EnsureItemIsValid() at Microsoft.SharePoint.SPListItem.PrepareItemForUpdate(Guid newGuidOnAdd, SPWeb web, Boolean bMigration, Boolean& bAdd, Boolean& bPublish, Object& objAttachmentNames, Object& objAttachmentContents, Int32& parentFolderId) at Microsoft.SharePoint.SPListItem.UpdateInternal(Boolean bSystem, Boolean bPreserveItemVersion, Guid newGuidOnAdd, Boolean bMigration, Boolean bPublish, Boolean bNoVersion, Boolean bCheckOut, Boolean bCheckin, Boolean suppressAfterEvents) at Microsoft.SharePoint.SPListItem.Update() at ASP._0443e3f9_0806_46ea_98ce_21e2d9f6c224_1904097461.btnDelete_Click(Object sender, EventArgs e) " Please help me How to delet all items from document library according to folderId


Solution

  • Try going backwards through the collection:

    for (int i = items.Count - 1; i >= 0; i--)
    {
        SPListItem item = items[i];
        item.Delete();
    }