Programmatically deleting list items/documents with Recycle Bin support


If you delete a document in SharePoint 2007 using the User Interface, it’s being moved to the Recycle Bin, so that it can be restored if necessary. There are situations when you want to include deleting list items and documents in your custom solutions. The most commonly used approach I’ve seen is calling the SPListItem.Delete() method. While this does the job, and deletes the item, it deletes it permanently instead of moving to the Recycle Bin.

Looking carefully at the SPListItem class, you can find the not-that-popular SPListItem.Recycle() method. Its description (“Recycles the item and returns the item’s GUID.”) is rather cryptic and doesn’t give you much clue on what you could use is for. It turns out that it’s exactly that method that you need to call in order to move a list item/document to the Recycle Bin instead of deleting it permanently.

In general moving items to the Recycle Bin instead deleting them permanently is what you should do in your custom solutions. It is standard SharePoint behavior and therefore something end users will expect of your solutions as well. You should perform the permanent deletion only if explicitly named in the requirements. Otherwise, let’s just stick to what SharePoint does to leverage the same User Experience.

Under the hood

Internally there isn’t much difference between the SPListItem.Delete and SPListItem.Recycle methods. Both call an internal SPListItem.Delete method with a different parameter which determines whether an item should be moved to the Recycle Bin or permanently deleted.

Technorati Tags: SharePoint, SharePoint 2007, MOSS 2007, WSS 3.0

Others found also helpful: