SharePoint 2010: PowerShell Script to Create SiteCollection using Custom Template

Scenario:

We need to write PowerShell script to create site collection using custom template.

Resolution:

First, we need to save site as template. For this, “Site Settings” -> “Save site as template”. Enter site template name, say, testCustomtemplate. After saving, go to “Solution Gallery” and download the testCustomtemplate.wsp.

Second, add the wsp. For this, run SharePoint PowerShell and run following commands:

Add-SPSolution [Path to testCustomtemplate.wsp]

Install-SPSolution –identity testCustomtemplate.wsp

Third, run following script making appropriate changes:

$newSite = New-SPSite -Name [SiteCollectionName] -Url [URL] -OwnerEmail [OwnerMail] -OwnerAlias [OwnerLogin] -SecondaryOwnerAlias [SecondaryOwnerLogin]

Enable-SPFeature -Identity [FeatureGuid] -Url $newSite.Url

$rootWeb = $newSite.RootWeb

$rootWeb.ApplyWebTemplate(“[CustomTemplate]”)

You need to find values of two variables. One is “FeatureGuid” and other is “CustomTemplate”.

FeatureGuid can be found out by simply going to 14 hives, go for folder containing the name “WebTemplate” along with your solution name and copy feature ID. Like we have feature ID {e27e2916-5eb2-4a23-901b-fb3f386136bb}.

CustomTemplate can be manually created by concatenating the name of the solution and feature as under:

{e27e2916-5eb2-4a23-901b-fb3f386136bb}# testCustomtemplate

Or run following simple script after activating the feature on site collection:

$site = Get-SPSite [SiteCollectionURL]

$web = $site.RootWeb

$templates = $web.GetAvailableWebTemplates(1033) //1033 is Culture ID.

Foreach($temaplte in $templates)

{

$template.Name

}

Advertisements

2 Comments »

  1. Please, can you be more specific about how to get the FeatureGuid value. What is “14 hives”? Where is the “WebTemplate” folder?

    Thank you in advanced
    L Santos

    • Farhan Faiz said

      if SharePoint is installed on C drive, this is the path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES”. Find your feature in the folder mentioned above.

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: