Partilhar via


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

Syntax

'Declaration
<SubsetCallableExcludeMemberAttribute(SubsetCallableExcludeMemberType.PerSpec)> _
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)
[SubsetCallableExcludeMemberAttribute(SubsetCallableExcludeMemberType.PerSpec)]
public void SaveAsTemplate(
    string strTemplateName,
    string strTemplateTitle,
    string strTemplateDescription,
    bool 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.

Remarks

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.

Examples

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("https://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
                        .Solutions
                        .Cast<SPUserSolution>()
                        .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....");
            Console.ReadLine();
        }
    }
}
Imports System
Imports Microsoft.SharePoint

Module ConsoleApp

    Sub Main()

        Using site As New SPSite("https://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

See Also

Reference

SPWeb Class

SPWeb Members

Microsoft.SharePoint Namespace

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