Monday, April 16, 2012

Creating multiple site collections in seperate databases and Managed Paths with PowerShell

The Below PowerShell Script to usefull to create Site Collection with diffrent Content Db also each site collection has a managed path (explicit inclusion) and some site collections are using diffrent Web templates.

************************************************************************
$ErrorActionPreference = "Stop"

# Creating site collections.
$WebApps = "http://siva:9999" # enter web aplication name Example http://<server>:<port>
$WebApps_url = "http://siva:9999" # enter web aplication URL Example http://<server>:<port>
$db_server = "siva\sharepoint" # Pass the Server name
$owner_alias = "siva\siva.reddy" #Pass the User Name
$sitecoll_templates = @("STS#0","STS#1","STS#2","MPS#1","BLOG#0")
# Foreach loop to create site collections in array.

$SitesToCreate = @("siva1","siva2","siva3","Spblog","Meeting")
$ID = 0
foreach ($site in $SitesToCreate)
{
    $ID += 1  
    Write-Host "Creating site" $site "( ID =" $ID ")"
    $site_collection_db = "Test_Content_"+$site 
    Write-Host "Site Collectie Database = "$site_collection_db
  
    # The sitename will be converted to lowercase for the managedpath. 
    $siteToLower = $site.ToLower()
    $SiteColl_URL = $WebApps_url + '/' + $siteToLower
    Write-Host "Site Collectie URL =" $SiteColl_URL
    Write-Host " "
 
    # Creating content database for each site.
    New-SPContentDatabase $site_collection_db -DatabaseServer $db_server -WebApplication $WebApps
    Write-Host -ForegroundColor Yellow "Content database" $site_collection_db "has been created."
 
    # Creating managed path for each site collection.
    New-SPManagedPath -RelativeURL $site -WebApplication $WebApps -Explicit
    Write-Host -ForegroundColor Yellow "Managed path" $siteToLower "has been created."
 
    Switch ($site)
    {
        "Meeting" { $sitecoll_templates = "MPS#1"}
        "Spblog" { $sitecoll_templates = "BLOG#0"}
        default { $sitecoll_templates = "STS#1" }
    }
 
    # Creating site collection with the name defined by the Switch
  
    New-SPSite $SiteColl_URL -OwnerAlias $owner_alias -ContentDatabase $site_collection_db -Name $sitename -Template $sitecoll_templates -Language 1033
    Write-Host -ForegroundColor Yellow "Site collection" $site "has been created with the name" $sitename
 
    # Change status of content database to Disabled so the next site collection can be created in a new Online database.
    Set-SPContentDatabase -Identity $site_collection_db -Status Disabled
    Write-Host -ForegroundColor Yellow "The status of content database" $site_collection_db "is Disabled."
  
}

# Change the status of all created content databases to Online.
foreach  ($site in $SitesToCreate)
{
    $site_collection_db = "SP_Content_"+$site
    Set-SPContentDatabase -Identity $site_collection_db -Status Online
    Write-Host -ForegroundColor Yellow "Status of database" $site_collection_db "is Online."
}

Tuesday, April 10, 2012

Site Collection Content migration from one server to another

Please go through the below step for migrating the content Database of web application from one to another server.

Pre-Requisites :-

  1. SiteCollection Content Backup.
  2. Custom Solutions.
  3. Version of SharePoint & SQL server

Step to Migrate Content DB :-
  1. Create Web application on new SP Server.
  2. Create Site Collection under Web application.
  3. Detach the database from SQL Server.
  4. Restore the database with same name on SQL Server.
  5. Run the below command's on powershell
  6. Dismount-SPContentDatabase "<ContentDb Name>"
  7. Mount-SPContentDatabase "<ContentDb>" -DatabaseServer "<DbServer>" -WebApplication "<web application url>"
  8. Change the Site collection administrator for Web Application
  9. Deploy the solution.