SPWeb.SaveAsTemplate method

Saves the website as a site template solution.

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

'Declaration
Public Sub SaveAsTemplate ( _
	strTemplateName As String, _
	strTemplateTitle As String, _
	strTemplateDescription As String, _
	fSaveData As Boolean _
)
'Usage
Dim instance As SPWeb
Dim strTemplateName As String
Dim strTemplateTitle As String
Dim strTemplateDescription As String
Dim fSaveData As Boolean

instance.SaveAsTemplate(strTemplateName, _
	strTemplateTitle, strTemplateDescription, _
	fSaveData)

Parameters

strTemplateName
Type: System.String

The name to use for the template on the Create form and also for the file name of the solution package (.wsp) that contains the template. If a solution package with the same file name already exists, a numeric suffix is added to the name when the solution file name is formed. For example, if you pass "Test", the method tries to create a template file named "Test.wsp". If that name is taken, it tries "Test2.wsp". If that name is in use, it tries "Test3.wsp" and so on.

strTemplateTitle
Type: System.String

A string that contains the display name of the site template.

strTemplateDescription
Type: System.String

A string that contains the description of the site template.

fSaveData
Type: System.Boolean

true if data on the website is saved as part of the site template; otherwise, false.

The SaveAsTemplate method saves a website as a site template solution in the Solution Gallery. Users can create new websites from the template by clicking New Site on the Site Actions menu.

Internally, this method calls the static method SPSolutionExporter.ExportWebToGallery and that method does the work.

The following example is a console application that saves the root website in a site collection as a site template solution. After saving the template, the example gets a reference to the user solution that contains the template and prints information about the solution to the console.

Imports System
Imports Microsoft.SharePoint

Module ConsoleApp

    Sub Main()

        Using site As New SPSite("http://localhost")
            Using web As SPWeb = site.RootWeb

                Dim templateName As String = "Test Site"
                Dim templateTitle As String = "Test Template"
                Dim templateDesc As String = "This template was saved programmatically."
                web.SaveAsTemplate(templateName, templateTitle, templateDesc, False)

                Dim solution As SPUserSolution = site.Solutions.Cast(Of SPUserSolution)().FirstOrDefault(Function(s) s.Name.StartsWith(templateName))

                If solution IsNot Nothing Then
                    Console.WriteLine("Solution name: {0}", solution.Name)
                    Console.WriteLine("Solution Id: {0}", solution.SolutionId)
                    Console.WriteLine("Status: {0}", solution.Status)
                End If

            End Using
        End Using

        Console.Write(vbCrLf & "Press ENTER to continue....")
        Console.Read()
    End Sub

End Module
Show: