kwizcom banner advertisement

SharePoint Online: Restore Deleted Subsite from Recycle Bin using PowerShell

Requirement: User had deleted a sub site by mistake. Now the requirement is to restore the deleted subsite in SharePoint Online.

How to restore deleted subsite in SharePoint?
To restore a deleted subsite in SharePoint, follow these steps: 
  • Login to SharePoint Online Site collection as 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 ( 2nd stage recycle bin direct URL: https://crescent.sharepoint.com/sites/Marketing/_layouts/15/AdminRecycleBin.aspx?view=13)
  • Click on "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 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
#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: Restore Deleted Subsite from Recycle Bin using PowerShell SharePoint Online: Restore Deleted Subsite from Recycle Bin using PowerShell Reviewed by Salaudeen Rajack on December 19, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.