SPWeb.SaveAsTemplate Method

Saves the website as a site template solution.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No

public void SaveAsTemplate(
	string strTemplateName,
	string strTemplateTitle,
	string strTemplateDescription,
	bool fSaveData


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.

Type: System.String

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

Type: System.String

A string that contains the description of the site template.

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....");