SPWeb.SaveAsTemplate 方法

Saves the website as a site template solution.

命名空间:  Microsoft.SharePoint
程序集:  Microsoft.SharePoint(位于 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, _
    fSaveData)
public void SaveAsTemplate(
    string strTemplateName,
    string strTemplateTitle,
    string strTemplateDescription,
    bool fSaveData
)

参数

  • strTemplateName
    类型: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
    类型:System.String

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

  • strTemplateDescription
    类型:System.String

    A string that contains the description of the site template.

  • fSaveData
    类型: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("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

另请参阅

引用

SPWeb 类

SPWeb 成员

Microsoft.SharePoint 命名空间

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