Enumerating SharePoint Site Templates for Site Collection with PowerShell

As part of my automated deployment post last week, one step of my deployment script called for creating a SharePoint site collection using the “STSADM –o createsite” command with the –sitetemplate option used for a specific site template.  If you have never had to create a site collection from a template at the command line you may not know what to put (or know what is available to you) for the –sitetemplate option.  To remedy this I created a very simple PowerShell script that opens a site collection and enumerates the usable site templates.

EnumerateSiteTemplates1

Note: The results listed will be specific to the site collection queried and will most likely be different from the templates in the global site template catalog listed when running STSADM –o enumtemplates.  Also note that some results (ex. STS and MPS) have multiple listings with a different # following them.  These are the same site template just with different build configurations specified.  Using this knowledge we can see that for instance the blank site and team site use the same template just with different components added or activated.  You can use this same capability in your own custom site templates if you are making multiple variations of a template.

Code Snippet:

$url = "http://hq.devone.local"

$site= new-Object Microsoft.SharePoint.SPSite($url )
$loc= [System.Int32]::Parse(1033)
$templates= $site.GetWebTemplates($loc)

foreach ($child in $templates)
{
    write-host $child.Name "  " $child.Title
}
$site.Dispose()

Enjoy the script and happy SharePointing adventures.

-Frog Out

6 thoughts on “Enumerating SharePoint Site Templates for Site Collection with PowerShell

  1. Originally posted on: https://briantjackett.com/archive/2009/08/18/enumerating-sharepoint-site-templates-for-site-collection-with-powershell.aspx#507954Hi,I am new to PowerShell Scripts in general and very very new to PowerShell with respect to SharePoint.After looking your script, I was curious to test in my sharepoint 2010 beta environment.But, I am getting below pasted Error;New-Object : Cannot find type [Microsoft.SharePoint.SPSite]: make sure the assembly containing this type is loaded.At C:ScriptsEnumeratingSharePointSiteTemplatesForSiteCollectionWithPowerShell.ps1:2 char:18+ $site= new-Object <<<< Microsoft.SharePoint.SPSite($url ) + CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommandYou cannot call a method on a null-valued expression.At C:ScriptsEnumeratingSharePointSiteTemplatesForSiteCollectionWithPowerShell.ps1:4 char:34+ $templates= $site.GetWebTemplates <<<< ($loc) + CategoryInfo : InvalidOperation: (GetWebTemplates:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNullYou cannot call a method on a null-valued expression.At C:ScriptsEnumeratingSharePointSiteTemplatesForSiteCollectionWithPowerShell.ps1:10 char:14+ $site.Dispose <<<< () + CategoryInfo : InvalidOperation: (Dispose:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNullCan you please guid me appropriately?

    Like

  2. Originally posted on: https://briantjackett.com/archive/2009/08/18/enumerating-sharepoint-site-templates-for-site-collection-with-powershell.aspx#508039Venkat, The issue you are seeing is because your PowerShell console has not loaded the appropriate SharePoint DLL (Microsoft.SharePoint.DLL) in order to call the API. There are a few ways to resolve this. One option is if you are running SharePoint 2010 you can just open the SharePoint 2010 Management Shell. This already loads the required references when you open it. Another option (not SharePoint 2010 specific) is to manually load the DLL by using the following command. [void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)Let me know if either works for you. Good luck.

    Like

  3. Originally posted on: https://briantjackett.com/archive/2009/08/18/enumerating-sharepoint-site-templates-for-site-collection-with-powershell.aspx#583131New-Object : Cannot find type [Microsoft.SharePoint.SPSite]: make sure the assembly containing this type is loaded.At H:sam.ps1:3 char:18+ $site= new-Object <<<< Microsoft.SharePoint.SPSite($url ) + CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommandYou cannot call a method on a null-valued expression.At H:sam.ps1:5 char:34+ $templates= $site.GetWebTemplates <<<< ($loc) + CategoryInfo : InvalidOperation: (GetWebTemplates:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNullYou cannot call a method on a null-valued expression.At H:sam.ps1:12 char:14+ $site.Dispose <<<< () + CategoryInfo : InvalidOperation: (Dispose:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNullcan any ody help me to resolve this

    Like

  4. Originally posted on: https://briantjackett.com/archive/2009/08/18/enumerating-sharepoint-site-templates-for-site-collection-with-powershell.aspx#583469Sam, Are you running this script on a machine that has the SharePoint DLLs (e.g. a server in the farm)? If so, running the command ‘[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)’ should take care of loading the assemblies and references that you are getting an error on.

    Like

Leave a comment