When the size of your site collections is increasing you sometimes have to move site collections around to put them in new content databases. There are a couple of different ways to perform a site collection move and I will try to cover some of them and end with a quite simple script that can make the work even easier.

So the first one out is the simple backup/restore stsadm operations. The problem with this is that when doing the restore it’s not possible to specify what database to restore it to. So to be able to do this you need to lock the other content databases except the one you want to add it to. This might not be possible and the you could use a maintenance farm.

A much better option is the mergecontentdbs stsadm operation that was introduced in SP1 of Microsoft Office SharePoint Server 2007 and allows 3 different options. The first option only analyse the situation and calculate the amount of site collections in the content database and their size. The second option is to move all sites in the specified source content database and move them to the specified destination content database. Our last option is to use an xml file containing the sites that we want to move as an input file to the command and the operation will only move the sites specified in that xml-file

The best thing with the last option is that we can use the enumsites operation to get an xml file of all sites for our a specific web application.

Example: stsadm -o enumsites -url [webAppUrl] -showlocks > "D:tempsites.xml"

The only problem with the above is that if you have a large amount of site collection and a number of content databases you will need to modify the list and remove the site collections that should not be moved to another content database. Later in this post I will show you how this problem can easily be addressed using PowerShell

Before using the mergecontentdbs operation you should consider two things. The first one is that since the mergecontentdbs operation is using the Content Deployment API it’s not suitable for sites larger then 10 GB. The other is that if content is changed during the operation you will not only risk losing the content but it could also cause database corruption as described in this KB: http://support.microsoft.com/kb/969242. So before moving the sites it’s a good recommendation to lock the site collections before the move.

To solve this I created a PowerShell script that does the following:

  1. Exports all the site collection for specific web application to an xml-file
  2. Remove all sites in the file that is not located in the database I have specified
  3. Sets the site collections to no access to prevent data loss
  4. Move the sites from the source database to the destination database using the xml file
  5. Offers you to make an IISreset on all WFEs in the farm (Recommended after the stsadm operation)
  6. Set the site collection lock back to it’s original
$env:PATH = $env:PATH +
";C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12BIN" 

$webAppUrl = "http://myWebAppUrl"
$sourceContentDB = "SourceContentDB"
$destinationContentDB = "DestinationContentDB"
$xmlfile = "d:tempsites.xml" 

# enumerate through all site collections and save them to the XML-file
Write-Host "Retreives all site collections located at"
$webAppUrl "and save it to " $xmlfile
stsadm -o enumsites -url $webAppUrl -showlocks > $xmlfile | out-null 

# Import the XML file content $sites = [xml](Get-Content $xmlfile)
# Remove only the site collections located in the source database
Write-Host "Cleaning up the" $xmlfile "file and remove sites that should not be affected"
$sites.Sites.Site | Where-Object {$_.ContentDatabase -ne $sourceContentdb} |
ForEach-Object { $_.ParentNode.RemoveChild($_) | out-null } 

# Enumerate through all the sites in the xml file and make necessary preparations 

foreach($site in $sites.sites.site) {
# Lock the sites to prevent errors when migration and data loss
Write-Host "Locking the site" $site.url "to prevent data loss"
stsadm -o setsitelock -url $site.URL -lock Noaccess
} 

# Moving sites
stsadm -o mergecontentdbs -url $webAppUrl -sourcedatabasename $sourceContentDB
  -destinationdatabasename $destinationContentDB -operation 3 -filename $xmlfile 

# Get the farm object to find get all the WFEs in the farm
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
Write-Host "To complete the move you need to make an IISreset on all WFEs in the farm"
Write-Host "You will now get the option to make an IISreset on all your WFEs"
Write-Host "" 

# Enumerate through all servers in the farm 

foreach ($svr in $farm.Servers) {
  foreach ($svc in $svr.ServiceInstances) {
    # If the server has the Windows SharePoint Services Web Application service it
    # is most likely it's a WFE.
    if($svc.TypeName -eq "Windows SharePoint Services Web Application"){
      # Ask if we should make an IISReset on the server
      $IISReset = Read-Host "Do you want to make an IIS-reset on"
      $svr.DisplayName "server (yes/No)?"
      if($IISReset.ToLower() -eq "yes") {
        iisreset $svr.DisplayName
      }
    }
  }
}
# Enumerate through all the sites in the xml file and finalize the move
foreach($site in $sites.sites.site) {
  # Set the original lockstate for the site collection
  Write-Host "Setting the original lock state for site" $site.url "to prevent data loss"
  stsadm -o setsitelock -url $site.URL -lock $site.Lock
}

You can download the script [Download not found]. Just make sure you run it with an account that has sufficient permissions as described in the enumcontentdbs TechNet article and to modify the first 4 paramaters.

Another option to move site collections is to use the Batch Site Manager that is included in the SharePoint Administration Toolkit. This tool is packaged as a solution that you deploy to your farm. When deployed (and activated), it will then have a new option in Central Administration under Application Management.

The tool allows you to very nicely see all site collection for each content database, select the ones you want to move and create a timer job for the move. The Batch Site Manager is using the stsadm operations backup and restore in the background. It usually takes a bit longer then the mergecontentdbs but you get the option to specify when the timer job should be run, where to store the temporary file and you can get a email notification telling you the status of the move. As you can see in the screenshot above the tool also offers you to batch locking and deletion of sites.

The Batch Site Manager is recommended to use with sites up to 15 GB. The mentioned recommendations for the both tools should be seen as recommendation and it’s possible to test your way forward and what works in your environment.

So what about really large site collections?
When moving really large sites none of the mentioned tools might work and you will then have to take on a different approach. In short this is what you need to do.

  1. Set the site collection that needs to be moved (farm 1) to read only and make a SQL backup of the content database (ContentDB A).
  2. Restore the content database with a new name (ContentDB B) and attach it to your Maintanence farm (Farm B) or if you don’t have one to a different web application. Make sure you use the –assignnewdatabaseid since we will later on restore the database back to it’s original location
  3. When it’s attached to farm B, use the stsadm -deletesite operation using the -gradualdelete parameter (introduced in the April CU) to remove the sites from ContentDB B that should not be moved.
  4. Backup the ContentDB B and restore it (but don’t attach it yet) to your Farm A
  5. Delete the large site from ContentDB A using stsadm and -gradualdelete.
  6. When the site is deleted you can attach ContentDB A to Farm A
  7. Unlock the site collection when done.

In SharePoint 2010 there is a new way to handle site templates, which I personally think is a very good improvement, and I will try to cover the changes here.

When saving a site as a site template (found in Site settings and Site Actions) it will in SharePoint 2010 be saved as a .wsp file and stored in the Site collections solution gallery. This makes it very easy to reuse and redeploy the site template to other site collections or to other farms. It also means that the Site template gallery we had in the previous version is no longer needed or available in SharePoint 2010.

When the solution is saved it will automatically get activated so that if you want to create a new sub site within your site collection the Site template will be available directly after it is saved. So when choosing Site actions and New site it will be available there as seen below.

So what about saving a Site collection as a site template? Well since end users are not able to create Site collections from the UI you need to find a way to apply your new site template without having it to be deployed globally. Microsoft has thought of this so when creating a Site collection from Central administration you have the option to select “select template later”.

This option does not only mean that when browsing to the site the first time you are able to select any of the available templates but you are able to upload a new Site template and by doing that the wsp will be saved in the new Site collection solution gallery. Quite neat isn’t it?

As you can see below, we are browsing to the new site and from there we have the link to our Site collection solution gallery. There we can upload the .wsp-file containing our site template. Next time we browse to the site collection root our new site template will be available under the custom tab.

It’s been a while since we had a usergroup in Gotheburg but now it’s time another one. This time it will be held at Altran the 8th of December and we will have two interesting sessions. The first one will be about Office365, held by Andre Henriksson from Microsoft. We will then listen to the [...]

One really good new feature in SharePoint 2010 for us admins are the Health Analyzer. From a set of health rules the Health analyzer checks on  regular basis for problems or potential issues in your farm. SharePoint is shipped with a bunch of rules checking for instance things like disk space, if application pool accounts [...]

When configuring Quota templates in SharePoint 2010 you will notice that a new section is added to the page. It’s called Sandbox solutions with Code Limits and is used to put a quota on sandbox solutions so that solutions that does not work properly will be shut down and not work when it reaches the maximum [...]

It’s an honor for us to be able to announce that our book PowerShell for Microsoft SharePoint 2010 Administrators is now published! This Friday it was published in the US and at www.amazon.com. Within the next couple of weeks it will also be available in the rest of the world; at book stores and at [...]