Saturday, October 30, 2010

SharePoint 2010 & PowerShell: Site Collections

This is the third in a series of posts on scripting administrative functions in SharePoint. I assume you have already created a farm and web application. In this post we will add three site collections to our web application.
First, let’s create a collaboration site for the HR group. This script is almost identical to the script we used to create the root site collection in the prior post. Once again, you will need to provide a name for the site, the email address and username of the site owner, and a URL. Notice that I have simply added “/sites/hr” to the end of the web application URL. By default, site collections are created beneath the managed path “/sites/”. You can create new managed paths using the New-SPManagedPath command. Also, notice that we are using the STS#0 site template. This template will create a team site. Here is a list of common templates that you can pick from:
  • STS#0 - Team Site
  • STS#1 - Blank Site
  • STS#2 - Document Workspace
  • MPS#0 - Basic Meeting Workspace
  • MPS#1 - Blank Meeting Workspace
  • MPS#2 - Decision Meeting Workspace
  • MPS#3 - Social Meeting Workspace
  • MPS#4 - Multipage Meeting Workspace
  • WIKI#0 - Wiki
  • BLOG#0 – Blog
$SiteName = "Human Resources Site"
$OwnerEmail = "USER_NAME@DOMAIN.com"
$OwnerAlias = "DOMAIN\USER_NAME"
$SiteURL = "http://intranet.DOMAIN.com/sites/hr"
$SiteTemplate = "STS#0"

Write-Host "Creating a relative site collection in the intranet web application"
New-SPSite -Url $SiteURL -owneralias $OwnerAlias -ownerEmail $OwnerEmail -Template $SiteTemplate
$Web = Get-SPWeb $SiteURL
$Web.title = $SiteName
$Web.update()
Let’s also create a blog where employees can share knowledge. Once again, we create a new site collection. We are concerned that we cannot predict how much content our employees will create. Therefore, we have decided to create another content database to keep the blog data separate from the formally sanctioned content on our intranet. There are a few additional steps required to do this. We will need to provide a name for the new content database and provide the URL of the web application that will own the new database. Remember that multiple site collections be assigned to a content database, but each database can only belong to one web application. The remainder of the script is identical to the one above, except that the New-SPSite command takes, as an additional parameter, the name of the content database.
$WebAppURL = "http://intranet.DOMAIN.com"
$ContentDatabase = "Content_Intranet_BLOG"

Write-Host "Creating a new content database in the intranet web application"
New-SPContentDatabase $ContentDatabase -WebApplication $WebAppURL

$SiteName = "Intranet Blog"
$OwnerEmail = "USER_NAME@DOMAIN.com"
$OwnerAlias = "DOMAIN\USER_NAME"
$SiteURL = "http://intranet.DOMAIN.com/sites/blog"
$SiteTemplate = "BLOG#0"

Write-Host "Creating a relative site collection in the intranet web application with a separate content database"
New-SPSite -Url $SiteURL -owneralias $OwnerAlias -ownerEmail $OwnerEmail -ContentDatabase $ContentDatabase -Template $SiteTemplate
$Web = Get-SPWeb $SiteURL
$Web.title = $SiteName
$Web.update()
Finally, let’s assume that the blog has been an overwhelming success (after all, social media projects within an enterprise are always successful, right?) In response, the boss has asked us to launch a WIKI. This is a high profile project and he wants the site to have its own URL. If we were using central administration, we would need to create a new web application for this. Using PowerShell, we can assign a custom URL to a Site Collection. This is called a host header site collection and requires significantly fewer resources than a web application. Microsoft uses this to host hundreds of different URL’s on SharePoint Online. This script is similar to the ones we have created already with a few small changes. Because the URL is not related to the web application URL, we need to tell SharePoint which web application to create our site collection in. This is done by passing an additional parameter to the New-SPSite command.
In addition, there seems to be a bug in the API and the binding is not created for our new site in IIS. Therefore, I have added a few additional lines to create the binding. Note that you need to pass the name of the web application we created in the previous post.
$SiteName = "Intranet WIKI"
$OwnerEmail = "USER_NAME@DOMAIN.com"
$OwnerAlias = "DOMAIN\USER_NAME"
$WebAppURL = "http://intranet.DOMAIN.com"
$SiteURL = "http://wiki.DOMAIN.com"
$SiteTemplate = "WIKI#0"

Write-Host "Creating a hostheader site collection in the intranet web application"
$WebApp = Get-SPWebApplication $WebAppURL
New-SPSite -url $SiteURL -HostHeaderWebApplication $WebApp -owneralias $OwnerAlias -ownerEmail $OwnerEmail -Template $SiteTemplate
$Web = Get-SPWeb $SiteURL
$Web.title = $SiteName
$Web.update()

$WebAppName = "Intranet Web Application"
$HostHeader = "wiki.DOMAIN.com"

Import-Module WebAdministration
New-WebBinding -Name $WebAppName -Port 80 -Protocol "http" -HostHeader $HostHeader
In the next post we will create an anonymous web application.

No comments:

Post a Comment