Export (0) Print
Expand All

SPWeb.ApplyWebTemplate Method (SPWebTemplate)

Windows SharePoint Services 3

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

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

public void ApplyWebTemplate(
	SPWebTemplate webTemplate


Type: Microsoft.SharePoint.SPWebTemplate
An SPWebTemplate object that represents the site definition or site template.

Caution noteCaution

Be careful about calling the ApplyWebTemplate method within a Web site 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 Web sites.

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

SPSite oSiteCollection = SPContext.Current.Site;
using(SPWeb oWebsite = oSiteCollection.OpenWeb("Website_URL"))
    SPWebTemplateCollection collWebTemplates = oSiteCollection.GetCustomWebTemplates(Locale_ID);
    SPWebTemplate oWebTemplate = collWebTemplates["Template_Name"];


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 Best Practices: Using Disposable Windows SharePoint Services Objects.

Community Additions

© 2014 Microsoft