共用方式為


SPWeb.SaveAsTemplate method

將網站儲存做為站台範本方案。

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

Syntax

'宣告
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
    Type: System.String

    若要使用上建立表單的範本,也包含範本的方案套件 (.wsp) 的檔案名稱的名稱。如果方案套件具有相同的檔案名稱已經存在,形成方案檔名加入數字尾碼至名稱。例如,如果您傳遞 「 測試 」,方法會嘗試建立一個名為"Test.wsp"的範本檔案。如果該名稱不會採取,它會嘗試"Test2.wsp"。如果正在使用該名稱,則會"Test3.wsp",依此類推。

  • strTemplateTitle
    Type: System.String

    字串,包含 「 網站 」 範本的顯示名稱。

  • strTemplateDescription
    Type: System.String

    字串,包含網站範本的描述。

  • fSaveData
    Type: System.Boolean

    true如果在網站上的資料儲存為組件的 「 網站 」 範本 ;否則, false。

備註

SaveAsTemplate方法會將網站儲存做為站台範本方案,方案組件庫中。使用者可以從此範本建立新的網站藉由按一下 [網站動作] 功能表上的 [新的站台

就內部而言,這個方法會呼叫靜態方法SPSolutionExporter.ExportWebToGallery和該方法運作。

Examples

下列範例會將根網站儲存做為站台範本方案,網站集合中的主控台應用程式。之後儲存範本,範例會取得使用者的解決方案,其包含範本並列印到主控台方案的相關資訊的參考。

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 class

SPWeb members

Microsoft.SharePoint namespace

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