Recycle Bin Search WebPart for Sharepoint
by Shoban • November 4, 2011 • SharePoint 2010 • 4 Comments
In this article we will see how we can develop a simple Visual WebPart which can be used to search Recycle Bin in Sites and Sub Sites using SPRecycleBinItemCollection and SPRecycleBinQuery.
1. Fire up Visual Studio and create a new Visual WebPart project.
2. Add a TextBox (txtTitle), Button (btnGo) and Div (results) to show our results.
3. Add the following code the Click event of the button
//Clear Results Table
results.InnerHtml = "";
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite("You Site URL"))
{
SPRecycleBinQuery query = new SPRecycleBinQuery();
query.ItemCollectionPosition = SPRecycleBinItemCollectionPosition.FirstPage;
query.IsAscending = true;
query.ItemState = SPRecycleBinItemState.FirstStageRecycleBin;
query.RowLimit = 100;
query.OrderBy = SPRecycleBinOrderBy.Title;
SPRecycleBinItemCollection binItems = site.GetRecycleBinItems(query);
//Filter Items where Title Maches the search text
var filteredItems = from i in binItems.OfType<SPRecycleBinItem>()
where i.ItemType == SPRecycleBinItemType.File && i.Title.ToUpper().Contains(txtTitle.Text.ToUpper())
select i;
results.InnerHtml = "<table class='SearchTable' width='100%'>";
if (filteredItems != null & filteredItems.Count() > 0)
{
results.InnerHtml += "<th>Search Results</th>";
foreach (var item in filteredItems)
{
results.InnerHtml += "<tr><td colspan='3'>" + item.Title + "</td></tr>";
}
}
else
{
results.InnerHtml += "<tr><td colspan='3'>No Results found!</td></tr>";
}
results.InnerHtml += "</table>";
}
});
4. Build and Deploy the WebPart. Below is a screenshot of this Web Part in action.

This is a simple example of how we can use c# to query Recycle Bin. We can improve this Web Part by adding Error Handling, Delete Permanently or Restore options etc.
Post inspired by this question in SharePoint.Stackexchange.com : http://sharepoint.stackexchange.com/questions/11005/is-it-possible-to-search-the-recycle-bins/11007#11007

It’s worth mentioning that you need to include Microsoft.SharePoint in your using block.
Also, it wasn’t obvious to me that the Div needs to be
Crap. I think I broke your comments by not intentionally breaking the HTML I posted.
Please check your email