Dump Recycle Bin Programmatically
At times, you may find that you wish to work with SharePoint recycle bins programmatically. I had a friend ask me this morning about approaches regarding dumping objects out of the recycle bin since he was experiencing storage issues disallowing deletion through the SharePoint web interface.
Working through the recycle bin through code is not a difficult task; there are a variety of object model pieces that you can use.
There are a few important ones:
SPRecycleBinItem – Represents an object in the Recycle Bin either at the site collection or site level
SPRecycleBinItemCollection – Represents a collection of SPRecycleBinItem objects
SPRecycleBinItemCollectionPosition – Represents the current page pointer of the recycle bin view
SPRecycleBinItemState – An enumeration containing the relevant SPRecycleBinItem object states, either FirstStageRecycleBin (site recycle bin) or SecondStageRecycleBin (site collection recycle bin)
SPRecycleBinItemType – An enumeration containing the SPRecycleBinItem object type, either:
SPRecycleBinOrderBy Specifies the piece of order data when executing a query against against the recycle bin.
SPRecycleBinQuery Specifies a query to run against the Recycle Bin in order to return SPRecycleBinItem objects
SPRecycleBinType An enumeration containing the type of the Recycle Bin, either:
So, how we can we use these to start working with the Recycle Bin? Fortunately, there are a variety of methods that the SharePoint API provides you in order to do so. For example, let’s assume that you wanted to dump all the SPRecycleBinItem objects for a site so that the Recycle Bin was then empty. We can build an SPRecycleBinItemCollection by creating a new SPWeb object which contains the RecycleBin collection, which allows a variety of method access. One of these methods includes the DeleteAll() method.
- using (SPSite site = new SPSite(http://sharepointserver))
- using (SPWeb web = site.OpenWeb())
- catch (Exception exception)