Tuesday, October 30, 2012

Restore a previously restored SharePoint Site Collection with Restore-SPSite

In SharePoint you get an error if you try to restore a site collection that war previously restored. I always make a site collection backup of a site while I'm developing directly on a site and if i make any mistakes I restore the back with the Powershell command Restore-SPSite.

Common error messages were:

Restore-SPSite : The operation that you are attempting to perform cannot be completed successfully.  No content databases in the
web application were available to store your site collection.  The existing content databases may have reached the maximum number
 of site collections, or be set to read-only, or be offline, or may already contain a copy of this site collection.  Create anoth
er content database for the Web application and then try the operation again.


The reason for this problem is that you can't restore sites or web which have the same id. SharePoint don't remove all ids when you delete a site collection. (Don't know why)


In all other blogs you'll see that you need to create a new content datase or a new webapplication. There is an easier way.

First check if the site is already deleted. When you delete a site collection without PowerShell parameter -GradualDelete $false the site is deleted gradually. That means a timer job will delete the site in near future but the site is marked a deleted.

To check if the site is already deleted use the PowerShell Command "Get-SPDeletedSite".

Now we'll find out for later which content database hosts the sitecollection. Select the "DatabaseId" and type following command:

Get-SPContentDatabase | Where {$_.Id -eq "02cf20b1-b312-4111-98b9-303b8113a71d" }

Pick the name of the content database for later.

If your site is listed there, delete the site now with the command:

Remove-SPDeletedSite -Identity 8489a07c-1b1a-485e-9e7d-b0c4ffbf26b5

The number after Identity is the SiteID.

Now you need to open the "SQL Server Management Studio" to login into the SharePoint Database. We not will change anything here. We use the Studio to find the deleted Site Id.

Click on the Content Database which contains the previously deleted site and open tables. Right click on "dbo.AllWebs" and select "Select Top 100 Rows".

In the table there is a "FullUrl" attribute. Search the url of your deleted site. If you found it copy the Guid in the ID Attribute.

Now open the "SharePoint 2010 Management Console" and enter following stsadm command:

stsadm -o deleteweb -webid <ID of  the Site> -databasename <Database Name> -databaseserver <DB Server>


stsadm -o deleteweb -webid E3E814F2-8819-4132-854B-7C7548EB2229 -databasename Main_Ct01 -databaseserver NT7262

Now you're able to restore your backuped site collection again.