This project has moved and is read-only. For the latest updates, please go here.

Separate Content Database Per Site Collection

Nov 30, 2011 at 5:03 PM

As per a Microsoft PFE recommendation I've made it a standard to have a separate content database for every site collection, but I found that the script does not allow for separate content databases at the site collection level. All site collections go to the same content database associated with the application. I did a little research and then made a customization to the script to allow me to specify a content database (or not) in the XML and the script creates the content database for the site collection. If a database is not specified it uses the application's content database.

There is only one problem with the change I've made and I couldn't find a way to resolve it. I do not know how to check for the existance of the content database before creating it. It's not a problem in my environment as every site collection has it's own content database, but if the script fails in the middle and I have to restart it, it gets messy. Also, if anyone else were to use it to assign a few site collections to the same content database, it's going to error each time it tries to create an already existing content database. I'm new to SharePoint and really new to PowerShell, and Google failed to turn up a method for checking for the existance of a content database.

The code I added/changed to the CreateWebApp function (inside AutoSPInstallerFunctions.ps1) is below, followed by an example XML:

if ($SiteCollection.databaseName -eq $null)
    $SiteCollectionDB = $database
    $SiteCollectionDB = $DBPrefix+$SiteCollection.databaseName
    Write-Host -ForegroundColor White " - Creating new content database: `"$SiteCollectionDB`"..."
    New-SPContentDatabase -Name  $SiteCollectionDB -WebApplication $WebAppName
# If a template has been pre-specified, use it when creating the Portal site collection; otherwise, leave it blank so we can select one when the portal first loads
If (($Template -ne $null) -and ($Template -ne "")) {
    $Site = New-SPSite -Url $SiteURL -OwnerAlias $OwnerAlias -SecondaryOwnerAlias $SecondOwnerAlias -ContentDatabase $SiteCollectionDB -Description $SiteCollectionName -Name $SiteCollectionName -Language $LCID -Template $Template -ErrorAction Stop
    $Site = New-SPSite -Url $SiteURL -OwnerAlias $OwnerAlias -SecondaryOwnerAlias $SecondOwnerAlias -ContentDatabase $SiteCollectionDB -Description $SiteCollectionName -Name $SiteCollectionName -Language $LCID  -ErrorAction Stop
<SiteCollection siteUrl="http://ecm.rmon.pri/Contracts"
name="Contracts ECM"
description="Corporate Contracts Management"
Dec 2, 2011 at 6:53 PM

How about this:

$result = Get-SPDatabase | Where-Object {$_.Name -like $SiteCollectionDB}

That will return something if that database exists. If it doesn't exist, it will be null. So, you could put a condition around it (for example: if ($result -eq $null){..create the database..}

Just an idea, could be a better way to do it but hopefully that gets you started.

I find it very peculiar though that the PFE is telling you to create a content database for each site collection. Perhaps you have some very special circumstances, but normally, you wouldn't do this. Its not even good practice, you'd end up with way too many databases.

Oct 9, 2013 at 3:58 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Marked as answer by brianlala on 11/3/2013 at 6:59 PM