Create a SiteCollection based on a WebTemplate from PowerShell

Hey! With SharePoint 2010 there is this cool new way of creating sites know as "WebTemplate". (see more about this here: http://msdn.microsoft.com/en-us/library/ms434313.aspx)

The thing is that as the "template" is only used at initialisation time then it is seen as created from the base template. So question: if I need to create the site from PowerShell what shall I specify as "-template" ?

The full command will be:
New-SPSite -Url $site_url -OwnerAlias $owner -Template "{Parent_Feature_GUID}#WebTemplate_Name"

IMPORTANT NOTE : If you have other WebTemplates enabled for creation from this WebTemplate that are based on native site definitions and if you have an OS with a different culture than the installed SharePoint the creation will fail.

Example:
I have a Win7 fr-FR with SharePoint 2010 en-US installed.
I created a WebTemplate for the root site based on "CMSPUBLISHING#0", then I created an other WebTemplate for the sub site that is also based on "CMSPUBLISHING#0".
I have a feature in the root site that enables the sub site template creation.
When I tried creating the site with the New-SPSite command it failed with the following message:
The SPWebTemplate named CMSPUBLISHING#0 cannot be found. The SPWebTemplate matching the BaseTemplateName and BaseConfigurationID of the WebTemplate declared by feature {GUID} could not be found.

Thanks to this guy, I managed to figure out it was due to a culture problem whereas SharePoint was pointing to "...\TEMPLATE\1036\..." in stead of "...\TEMPLATE\1033\..." to find the CMSPUBLISHING template.

Easiest solution found: Execute the command within a en-US (1033) culture context !
Here it is (credits for the Using-Culture here):

$site_url = "http://mysitecollection";
$owner = "me";


function Using-Culture (
  [System.Globalization.CultureInfo]
  $culture = (throw "USAGE: Using-Culture -Culture culture -Script {...}"),
  [ScriptBlock]
  $script = (throw "USAGE: Using-Culture -Culture culture -Script {...}"))
{
    $OldCulture = [Threading.Thread]::CurrentThread.CurrentCulture
    $OldUICulture = [Threading.Thread]::CurrentThread.CurrentUICulture    
    try {        
        [Threading.Thread]::CurrentThread.CurrentCulture = $culture        
        [Threading.Thread]::CurrentThread.CurrentUICulture = $culture        
        Invoke-Command $script    
    }    
    finally {        
        [Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture        
        [Threading.Thread]::CurrentThread.CurrentUICulture = $OldUICulture    
    }
}

Remove-SPSite -Identity $site_url -GradualDelete -Confirm:$False;

$culture = [System.Globalization.CultureInfo]::GetCultureInfo(1033);

Using-Culture -culture $culture -script { 
    New-SPSite -Url $site_url -OwnerAlias $owner -Template "{FEATURE_GUID}#WebTemplateName";
};

Comments