SPWeb.SaveAsTemplate method

Saves the website as a site template solution.

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


Public Sub SaveAsTemplate ( _
    strTemplateName As String, _
    strTemplateTitle As String, _
    strTemplateDescription As String, _
    fSaveData As Boolean _
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, _
public void SaveAsTemplate(
    string strTemplateName,
    string strTemplateTitle,
    string strTemplateDescription,
    bool fSaveData


  • 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.

using System;
using System.Linq;
using Microsoft.SharePoint;

namespace ConsoleApp
    class Program
        static void Main(string[] args)
            using (SPSite site = new SPSite("http://localhost"))
                using (SPWeb web = site.RootWeb)
                    string templateName = "Test Site";
                    string templateTitle = "Test Template";
                    string templateDesc = "This template was saved programmatically.";
                    web.SaveAsTemplate(templateName, templateTitle, templateDesc, false);

                     SPUserSolution solution = site
                        .FirstOrDefault(s => s.Name.StartsWith(templateName));

                    if (solution != null)
                        Console.WriteLine("Solution name: {0}", solution.Name);
                        Console.WriteLine("Solution Id: {0}", solution.SolutionId);
                        Console.WriteLine("Status: {0}", solution.Status);
            Console.Write("\nPress ENTER to continue....");
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....")
    End Sub

End Module

See also


SPWeb class

SPWeb members

Microsoft.SharePoint namespace

ExportWebToGallery(SPWeb, String, String, String, SPSolutionExporter.ExportMode, Boolean)