Recover SP Document from Unattached ContentDB

As you may already know, SharePoint has own Recycle Bin as first-stage for site and second-stage for site collection. Intentionally or accidentally deleted files are still reachable in those stages to recover to original place for 93 retention days.

This post’s mission starts with after that retention days missed, fortunately there is still hope thanks to PowerShell 🙂 But you should have regular SQL backup of content databases of SharePoint environment which I believe this is crucial and assume you already do it.

Recover related content database that deleted files site collection runs on, with like XXX_ContentDB_Restored to you AGs or specific SQL server to point possible presence date of deleted documents.
Use that information gathered from DB team, Database Server and Content DB Name, in the following script’s variables.

*Farm administrator and db owner rights are essential
*Please run script line by line and be aware what is going on there

################################
# Author: E.Ayyildiz
# Date: 08/03/2021
# Version: 1.0.1
################################

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables that will be used in script
$DatabaseServer = "TESTSQLDBSERVER1"
$DatabaseName = "TEST_ContentDB_Restored"

#Assign Unattached DB to variable
$db = Get-SPContentDatabase -ConnectAsUnattachedDatabase -DatabaseServer $DatabaseServer -DatabaseName $DatabaseName
$db.Sites #There may be a lot of sites inside content db, pay attention and filter with following line below
$site = Get-SPSite -ContentDatabase $db -Limit ALL | ? {$_.URL -like "https://xxx.yy/erdemayyildiz"}
 
$web=$site.AllWebs
$list = $web.Lists["Documents"]
$listitems = $list.Items
 
#Restore multiple files that in specific list
foreach($listlitle in $listitems){
 
$list = $web.Lists[$listlitle]
$SaveLocation = "C:\Temp\" + $listlitle.Name
$binary = $ListTitle.File.OpenBinary()
$stream = New-Object System.IO.FileStream(($SaveLocation), [System.IO.FileMode]::Create)
$writer = New-Object System.IO.BinaryWriter($stream)
$writer.Write($binary)
$writer.Close()

}

Hope it works for you.

Erdem Ayyildiz

About the Author

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir