ApplyWebTemplate method (SPWebTemplate)
Collapse the table of content
Expand the table of content

SPWeb.ApplyWebTemplate method (SPWebTemplate)

Applies the specified site definition object or site template object to the website that has no template applied to it.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Public Sub ApplyWebTemplate ( _
	webTemplate As SPWebTemplate _
Dim instance As SPWeb
Dim webTemplate As SPWebTemplate



Type: Microsoft.SharePoint.SPWebTemplate

The site definition or site template to be applied.


webTemplate is null .

This method calls the ApplyWebTemplate(String) method with the Name property of the webTemplate parameter.

Caution noteCaution

Be careful about calling this method within a website provisioning callback function. If you call this method inside a provisioning callback function that is defined within the same site definition configuration that is being applied, you can cause an infinite loop. Instead, create two similar site definition configurations within the site definition, one that is visible and one that is hidden. The configuration can then contain a provisioning assembly callback function that applies the hidden configuration to websites.

The following code example applies a specified site template object to a subsite in the current site collection.

Dim siteCollection As SPSite = SPContext.Current.Site
Using webSite As SPWeb = siteCollection.OpenWeb("Website_URL")
    Dim webTemplates As SPWebTemplateCollection = 
    Dim webTemplate As SPWebTemplate = webTemplates("Template_Name")
End Using

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

© 2016 Microsoft