• Recycle Bin Search WebPart for Sharepoint

    by  • 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

    About

    Shoban Kumar is currently working in an MNC in Trivandrum as a System Analyst. He actively writes .net articles in his blog http://www.codegeeks.net and http://www.dotnetcurry.com. He has also written many open source applications which can be found in Codeplex : http://www.codeplex.com/site/users/view/shobankr You can also follow him in Twitter @shobankr

    4 Responses to Recycle Bin Search WebPart for Sharepoint

    1. Jon
      November 8, 2011 at 8:41 pm

      It’s worth mentioning that you need to include Microsoft.SharePoint in your using block.

    2. Jon
      November 8, 2011 at 9:24 pm

      Also, it wasn’t obvious to me that the Div needs to be

      • Jon
        November 8, 2011 at 9:26 pm

        Crap. I think I broke your comments by not intentionally breaking the HTML I posted.

        • Shoban
          November 9, 2011 at 3:34 pm

          Please check your email :)

    Leave a Reply

    Your email address will not be published. Required fields are marked *