Spike on SharePoint

Restoring Deleted Site Collections with SharePoint

Here's a step-by-step walkthrough on how to restore lost site collection.

In the course of managing site collections it occasionally becomes necessary to delete a site collection. In the course of deleting site collections, it occasionally becomes necessary to restore a deleted site collection. Ever since SharePoint 2010 Service Pack 1, this has been possible using the Restore-SPDeletedSite commandlet. 

In this article I will complete the following steps:

  1. Create 10 new site collections.
  2. Delete one of them using central administration.
  3. Restore the deleted site collection.

I will do all of this (except deleting the site collection) using the SharePoint 2013 Management shell.

Create 10 new Site Collections
In my demo environment I have a web application with the URL http://sharepoint.local. I have added the wildcard managed path "del" and I will place all the site collections under this managed path. This way I keep everything separate from my other content so I don't accidentally go deleting a bunch of stuff I didn't mean to, in which case I would probably read this article (LOL).

I will go ahead and create an array containing the following strings:

"deleteMe1"
"deleteMe2"
"Woops"
"deleteMe3"
"deleteMe4"
"dleteMe5"
"deleteMe6"
"deleteMe7"
"deleteMe8"
"deleteMe9"

(You can probably guess which one I am going to use for the site collection I want to restore). 

To do this I first open up the SharePoint 2013 Management Shell (I have it pinned to my taskbar so I simply Right Click the SharePoint 2013 Management Shell icon and choose "Run as Administrator."  This ensures it's running as administrator.

I am a farm administrator, and I am an SPShell admin on everything in my development environment so I shouldn't have any permission issues.

Next, create the new Array $names and populate it with the collection of strings by typing the following:

\ Now that I have my environment how I like it and I have an array ready to go ($names), I type the following into the shell, which will loop through the $names array and create a new site collection for each one:

foreach($s in $names){New-SPSite http://sharepoint.local/del/$s –Name $s –OwnerAlias [email protected] –Template STS#0}

You can see by the image that the text wrapping will do what it wants to do in the actual shell:

Here's what this did:

  • foreach($s in $names): Sets up a loop for each item in the $names collection. In this case, each $s will be a site collection.  The { opens up the code block. In other words, everything between the {    } will be executed on each of the items in the $names collection, which in this case are each of the 10 site collections I created earlier.
  • New-SPSite: This commandlet creates a new site collection. The parameters I pass in are the URL for the new site collection (I can just type in the URL the Commandlet will recognize what it is).  I end the URL with the $s variable created in the foreach loop.  This variable holds each site collection as the loop goes through them.  This allows me to end the URL parameter in the new-SPSite commandlet with a $s (which will be replaced with the site collections name).  In other words as it loops through, $s will hold
    "deleteMe3," "deleteMe4," etc., so that the URLs being attached to each site collection match the value of http://sharepoint.local/del/$s.
  • The OwnerAlias parameter: The primary site collection administrator .
  • The –Template parameter: Tells SharePoint which template to base the top level site of the new site collection on. In this case I pass in the STS#0 code (which is the SharePoint 2013 team site template code).

I'm going to put these new site collections into a variable $sc in order to be able to work with them as a group. Since I created the wildcard managed path del for this example and I know that there are no other site collections that will answer to this request in my environment, I can simply pass in the * wildcard character to get the newly created site collections into the variable $sc by typing the following into the Management Shell:

$sc = Get-SPSite http://sharepoint.local/del/*

At this point to verify that $sc has what I want it to have I type $sc into the Management Shell and get this:

The order of the results let me know that the site collection answering to the URL http://sharepoint.local/del/Woops is the last one in the collection.

I know that there are 10 site collections in $sc, but if I wanted to make sure, I could always use the length property of the array $sc by typing $sc.Length into the shell.

With 10 items in the collection I can use the index of the position for the http://sharepoint.local/del/Woops site collection. Since it is the last one, and the indexes are 0 based (start at 0 not 1), I know that I can put the site collection into its own variable ($woops) with the following entry into the Shell:

$woops = Get-SPSite $sc[9]

I can verify that it worked by typing $woops into the shell like so:

Delete One Using Central Administration
I'm going to delete this site collection using central administration so that I can recover it using the Restore-SPDeletedSite commandlet. If I were to delete this site collection using the Remove-SPSite commandlet I would not be able to recover it in this way.

First, open the central administration and click the "Application Management" link:

Then click the "Delete a site collection" link:

I click the "Select a site collection" link (no selection) and then click the "Change site collection" button.

Then, find the http://sharepoint.local/del/Woops site collection and select it and click OK:

Next, click the "Delete" button and hit OK:

After that's done, go back to the SharePoint 2013 Management Shell and type Get-SPSite http://sharepoint.local/del* to make sure the site collection is gone:

As you can see, it's no longer there.

Restore the Deleted Site Collection
Type the following commandlet into the shell to see all the site collections in the farm that have been deleted and are available to be recovered using the Restore-SPDeletedSite Commandlet:

Get-SPDeletedSite

I'm going to use the Path property to put this deleted site collection into a variable. To do this I type the following into the management shell:

$delWoops = Get-SPDeletedSite '/del/Woops'

To verify that this works, type $woops into the shell:

Now go ahead and restore the deleted site collection like so: (I type Y to the prompt):

Restore-SPDeletedSite $delWoops

To verify that this worked I go ahead and type Get-SPSite http://sharepoint.local/del*.

 

Just to make sure, go ahead and launch the site collection in the browser with the following entry into the shell:

Start-Process iexplore.exe http://sharepoint.local/del/Woops

This opens the site collection in the nrowser and lets me know that it all worked!

There are many, many factors to consider when doing this, and it is never a replacement for a solid disaster recovery process. However it is a great trick to have in your toolbox.

About the Author

Spike is a full-time trainer and consultant with Transmission I.T. He began his career in computers in the early 1980s with Fortran IV. Sometime in 2007, he found SharePoint and has been working with it ever since. His interests are SharePoint, HTML 5, JavaScript and ASP.NET. He was the head of the Arizona SharePoint Pros user group for the last few years and speaks at conferences and user groups.

Featured

comments powered by Disqus

Subscribe on YouTube