Share via


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)

Syntax

'Declaration
Public Sub ApplyWebTemplate ( _
    webTemplate As SPWebTemplate _
)
'Usage
Dim instance As SPWeb
Dim webTemplate As SPWebTemplate

instance.ApplyWebTemplate(webTemplate)
public void ApplyWebTemplate(
    SPWebTemplate webTemplate
)

Parameters

Exceptions

Exception Condition
ArgumentNullException

webTemplate is null .

Remarks

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

Warning

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.

Examples

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 = 
        siteCollection.GetCustomWebTemplates(Locale_ID)
    Dim webTemplate As SPWebTemplate = webTemplates("Template_Name")
    webSite.ApplyWebTemplate(webTemplate)
End Using
SPSite oSiteCollection = SPContext.Current.Site;
using(SPWeb oWebsite = oSiteCollection.OpenWeb("Website_URL"))
{
    SPWebTemplateCollection collWebTemplates = oSiteCollection.GetCustomWebTemplates(Locale_ID);
    SPWebTemplate oWebTemplate = collWebTemplates["Template_Name"];

    oWebsite.ApplyWebTemplate(oWebTemplate);
}

Note

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.

See also

Reference

SPWeb class

SPWeb members

ApplyWebTemplate overload

Microsoft.SharePoint namespace