Export (0) Print
Expand All

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 void ApplyWebTemplate(
	SPWebTemplate webTemplate
)

Parameters

webTemplate
Type: Microsoft.SharePoint.SPWebTemplate

The site definition or site template to be applied.

ExceptionCondition
ArgumentNullException

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.

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);
}
NoteNote

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.

Show:
© 2014 Microsoft