kwizcom banner advertisement

Search SharePoint Recycle bin with SPRecycleBinQuery

Searching inside SharePoint recycle bin for a deleted item or file, is a pain especially when you have LOT of deleted items and files in SharePoint recycle bin. Unfortunately SharePoint doesn't provide Search capability in recycle bin and you have to press Next>> <<Prev buttons to search for your file.

We can perform Search on SharePoint Recycle bin items with SPRecycleBinQuery. Here is an example of SPRecycleBinQuery with PowerShell.

Good news is: If you know your file name, can search inside recycle in programmatically with PowerShell!

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

#Get the Site collection
$site = new-object Microsoft.SharePoint.SPSite("http://sharepoint.company.com")

#Create new object for SPRecycleBinQuery
$query = new-object Microsoft.SharePoint.SPRecycleBinQuery

#$query.ItemState = "FirstStageRecycleBin"
$query.ItemState = "SecondStageRecycleBin"

#How many Rows to be returned
$query.RowLimit = 100

#Call GetRecycleBinItems to Search Inside SharePoint Recycle Bin
$DeletedItemsColl = $site.GetRecycleBinItems($query)

#Filter Result
$Result= $DeletedItemsColl | where  {$_.Title -match ".txt"}

#Write output
$Result | foreach-object {
    (Write-Host "Found Item:" $_.Title)
    (write-host "Created by:" $_.AuthorName)
    (write-host "Deleted by:" $_.DeletedByName)
    (write-host  "Deleted Date:"$_.DeletedDate)
  (write-host  "File Size(Bytes):" $_.Size)
  (write-host  "Original Location:" $_.DirName)
  (write-host "Web:" $_.web.URL)
 (Write-Host "------------------------------------")
  }
 
We can search recycle bin items based on File Name, Deleted By, Deleted Time, Size, etc. Its also possible to either restore or permanently delete the files by calling approximate method.This is extremely useful if you want to search recycle bin programmatically!
Search SharePoint Recycle bin with SPRecycleBinQuery Search SharePoint Recycle bin with SPRecycleBinQuery Reviewed by Salaudeen Rajack on 5:21 PM Rating: 5

2 comments:

  1. Don't forget to add this at the bottom: $site.Dispose()

    ReplyDelete
  2. Thanks for this, this saved my life 3 years later... the C# code for this
    SPSite site = new SPSite("https://portal.denallix.com");
    site.RecycleBin.DeleteAll();
    site.Dispose();
    for anyone who was looking for this

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.