SharePoint Online: Restore Deleted Subsite from Recycle Bin using PowerShell

Requirement: The user had deleted a subsite by mistake. Now the requirement is to restore the deleted subsite in SharePoint Online.

How to restore a deleted subsite in SharePoint?
Deleted subsites in SharePoint Online are moved to the second-stage recycle bin and can be restored by the Site Collection Administrators. To restore a deleted subsite in SharePoint, follow these steps: 
  • Login to SharePoint Online Site collection as a site collection administrator
  • Navigate to Site Settings >> Click on "Recycle bin" under site collection administration
  • Click on "Second-stage recycle bin" link and select the desired subsite by clicking the checkbox of the subsite (Second stage recycle bin direct URL: https://<tenant>.sharepoint.com/sites/Marketing/_layouts/15/AdminRecycleBin.aspx?view=13)
  • Click on the "Restore" button in the ribbon to start restoring the subsite.
sharepoint online restore subsite from recycle bin

By default, deleted items are retained for 93 days in the recycle bin. If you don't find your subsite under the recycle bin - Your site may be completely deleted! So you are left with restoring the content database backups in SharePoint On-premises environment or raising a support ticket with Microsoft for SharePoint Online.

Restore Deleted Subsite from Recycle Bin using PowerShell
Here is how to restore deleted subsite in SharePoint Online using PowerShell
#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/Marketing"
$DeletedSubsiteURL="/sites/marketing/2018/docs"
 
#Get the credentials to connect 
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
Try {    
    #Setup the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $Credentials
     
    #Get the Admin recycle bin
    $Site = $Context.Site
    $RecycleBinItems = $Site.RecycleBin
    $Context.Load($Site)
    $Context.Load($RecycleBinItems)
    $Context.ExecuteQuery()
 
    #Frame DirName (Parent Site URL) and LeafName (Subsite URL)
    $DirName=$DeletedSubsiteURL.Substring(1,$DeletedSubsiteURL.LastIndexOf("/")-1)
    $LeafName=$DeletedSubsiteURL.Substring($DeletedSubsiteURL.LastIndexOf("/")+1)

    #Get the deleted subsite from Recycle bin
    $DeletedSubsite = $RecycleBinItems | Where { ($_.DirName -eq $DirName) -and ($_.LeafName -eq $LeafName)}
    If($DeletedSubSite -ne $Null)
    { 
        #Restore the first subsite found from Recylce bin
        $DeletedSubSite[0].Restore()
        $Context.ExecuteQuery()
        Write-Host -f Green "Deleted Subsite restored Successfully!"
    }
    else
    {
        Write-Host -f Yellow "Deleted Subsite Not Found in Recycle Bin!"
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

SharePoint Online: PnP PowerShell to Restore Deleted SubSite
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/sites/Marketing"
$SiteTitle="Branding"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get Deleted Subsite
$DeletedSite = Get-PnPRecycleBinItem | Where { $_.Title -eq $SiteTitle -and $_.ItemType -eq "Web"}

#Restore Subsite
If($DeletedSite)
{
    Restore-PnPRecycleBinItem -Identity $DeletedSite -Force -ErrorAction Stop
    Write-Host -f Green "Subsite '$SiteTitle' restored successfully!"
}
Else
{
    Write-host -f Yellow "Could not Find deleted Subsite:"$SiteTitle
} 

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.