Thursday, May 9, 2013

How to create a site collection and specify the content database

So pretty much everyone knows how to create a new site collection, but what is not so simple is creating a new site collection in a content database which you specified. This can easily be achieved using some powershell, as shown below:


$SITECOLLECTION = Read-Host "What is the name of the site collection you wish to add?"
$SITEDESCRIPTION = Read-Host "What is the site collection description?"
$SITEURL = Read-Host "What is the URL of the site you wish to add?"
$SITEADMINISTRATOR = Read-Host "Who is the site collection administrator?"
$DATABASENAME = Read-Host "In which content database do you wish to add this site collection?"
$SITETEMPLATE = "CMSPUBLISHING#0" # --> THIS IS SITE TEMPLATE FOR PUBLISHING SITE

# CHECK IF CONTENT DATABASE EXIST

$CONTENTDB = Get-SPContentDatabase -identity $DATABASENAME

# IF CONTENT DB EXISTS WE WILL ADD SITE COLLECTION AND USE CONTENT DB SPECIFIED ELSE DISPLAY ERROR MSG

If (!$CONTENTDB)
{
Write-Host "The specified content database has not been found - PLEASE TRY AGAIN"
}
else
{
Write-Host "SITE COLLECTION CREATION STARTED"

New-SPSite -Name $SITECOLLECTION -Url $SITEURL -Template $SITETEMPLATE -OwnerAlias $SITEADMINISTRATOR -ContentDatabase $CONTENTDB -Description $SITEDESCRIPTION

Write-Host "SITE COLLECTION CREATION COMPLETE"

}

I hope this helps you as it did me.