Recycle Bin Search WebPart for Sharepoint

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

[sourcecode language="csharp"]
//Clear Results Table
results.InnerHtml = "";
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>";
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 :

5 thoughts on “Recycle Bin Search WebPart for Sharepoint

  1. This post is very interesting but it took me a long time to find it in google.

    I found it on 18 spot, you should focus on quality
    backlinks building, it will help you to rank to google top 10.
    And i know how to help you, just type in google – k2 seo tips

Comments are closed.