Home Contact

The Frog Pond of Technology

Ripples of Knowledge for SharePoint and Other .Net Technologies

News

My blog has moved to WordPress at BrianTJackett.com. Any pages will automatically redirect to the new address in 7 seconds. Please excuse an issues during this migration.

 Subscribe to this blog


About Me

Name:
Brian T. Jackett
Location:
Columbus, OH
Company:
Microsoft

Find me on...

Tag Cloud


Archives

Post Categories

Syndication:

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


Tuesday, August 18, 2009 1:12 PM

Feedback

# re: Enumerating SharePoint Site Templates for Site Collection with PowerShell

Hi,

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:\Scripts\EnumeratingSharePointSiteTemplatesForSiteCollectionWithPowerShell.ps1:2 char:18
+ $site= new-Object <<<< Microsoft.SharePoint.SPSite($url )
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

You cannot call a method on a null-valued expression.
At C:\Scripts\EnumeratingSharePointSiteTemplatesForSiteCollectionWithPowerShell.ps1:4 char:34
+ $templates= $site.GetWebTemplates <<<< ($loc)
+ CategoryInfo : InvalidOperation: (GetWebTemplates:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull


You cannot call a method on a null-valued expression.
At C:\Scripts\EnumeratingSharePointSiteTemplatesForSiteCollectionWithPowerShell.ps1:10 char:14
+ $site.Dispose <<<< ()
+ CategoryInfo : InvalidOperation: (Dispose:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Can you please guid me appropriately?


3/6/2010 5:55 AM | Venkatanarasimha KL

# re: Enumerating SharePoint Site Templates for Site Collection with PowerShell

Venkat,

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. 3/6/2010 8:08 PM | Brian Jackett

# re: Enumerating SharePoint Site Templates for Site Collection with PowerShell

I am getting below pasted Error;
but thanks for share 7/13/2010 11:11 PM | worldcupjersey

# re: Enumerating SharePoint Site Templates for Site Collection with PowerShell

New-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.NewObjectCommand

You 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 : InvokeMethodOnNull


You cannot call a method on a null-valued expression.
At H:\sam.ps1:12 char:14
+ $site.Dispose <<<< ()
+ CategoryInfo : InvalidOperation: (Dispose:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

can any ody help me to resolve this 6/23/2011 2:51 PM | sam

# re: Enumerating SharePoint Site Templates for Site Collection with PowerShell

Sam,

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.

6/26/2011 11:52 PM | Brian T. Jackett

# re: Enumerating SharePoint Site Templates for Site Collection with PowerShell

Hi Brian Jackett,

Thanks It's works for me :)
4/9/2014 5:48 AM | Amit Kumar Vemra

Post A Comment
Title:
Name:
Email:
Comment:
Verification: