Programmatically Manage SharePoint Recycle Bin

SharePoint object model allows to access recycle bins of web sites or site collections by calling RecycleBin property of the SPWeb or SPSite classes respectively.

[crayon lang=”c#”]
SPRecycleBinItemCollection recycleBinItemcollection = yourSite.RecycleBin;
//or
SPRecycleBinItemCollection recycleBinItemcollection = yourWeb.RecycleBin;
[/crayon]

RecycleBin property will return recycle bin items as SPRecycleBinItemCollection. Items of this collection is SPRecycleBinItem.

Type of the recycle bin can be accessed by BinType property of SPRecycleBinItemCollection. It will return SPRecycleBinType enumaration which has following members.

NoRecycleBin Indicates that the type of the recycle bin is not specified.
RecycleBin Specifies the end-user recycle bin. Value = 1.
SiteRecycleBin Specifies the site-collection recycle bin. Value = 2.

Recycle bin type is important because SharePoint 2010 has 2-stage recycle bin structure. When a user deletes an item, the item is automatically sent to the first-stage (end-user or web) Recycle Bin. By default, when an item is deleted from the first-stage Recycle Bin, the item is sent to the second-stage (site collection) Recycle Bin. A site collection administrator can restore items from the second-stage Recycle Bin.

Items in SPRecycleBinItemCollection can be deleted permanantly by using Delete or DeleteAll methods. Delete method takes Guid array of the items that are wanted to be deleted. Don’t forget that these methods delete items permanantly. If you want to move items from first-stage recycle bin to second-stage recycle bin, you can use MoveToSecondStage or MoveAllToSecondStage methods.

For restoring items in the recycle bin, Restore and RestoreAll methods can be used. These methods will restore Recycle Bin items to their original locations.

As I mentioned before SPRecycleBinItems collection is a collection of SPRecycleBinItem objects. SPRecycleBin type has some useful members as follows:

Author Gets the user who originally created the item.
AuthorEmail Gets the e-mail address of the user who originally created the item.
AuthorId Gets the SharePoint Foundation user ID of the user who originally created the item.
AuthorName Gets the display name of the user who originally created the item.
DeletedBy Gets the user who deleted the item.
DeletedByEmail Gets the e-mail address of the user who deleted the item.
DeletedById Gets the ID of the user who deleted the item.
DeletedByName Gets the display name of the user who deleted the item.
DeletedDate Gets the date and time that the item was deleted.
DirName Gets the Web site–relative URL of the list or folder that originally contained the item.
ID Gets the GUID of the item.
ImageUrl Gets the server-relative URL to the icon of the item.
ItemState Gets the Rrecycle Bin stage of the item.
ItemType Gets the type of the Recycle Bin item.
LeafName Gets the leaf name of the item.
ProgId Gets the program identifier (ID) for the item.
Size Gets the size of the item in bytes.
Title Gets the title of the item.
Web Gets the Web site from which the item was deleted.

Similar to SPRecycleBinItemCollection class, there are Delete, MoveToSecondStage and Restore methods of SPRecycleBinItem class.

More information about SPRecycleBinCollection you can check related MSDN context: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.sprecyclebinitem_members.aspx

Leave a comment

Your email address will not be published.

%d bloggers like this: