Compartilhar via


SPSolutionExporter.ExportWebToGallery Method

Exports the specified Web site as a Web template in the Solution Gallery.

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

Syntax

'Declaration
Public Shared Function ExportWebToGallery ( _
    web As SPWeb, _
    solutionFileName As String, _
    title As String, _
    description As String, _
    exportMode As SPSolutionExporter.ExportMode, _
    includeContent As Boolean _
) As String
'Usage
Dim web As SPWeb
Dim solutionFileName As String
Dim title As String
Dim description As String
Dim exportMode As SPSolutionExporter.ExportMode
Dim includeContent As Boolean
Dim returnValue As String

returnValue = SPSolutionExporter.ExportWebToGallery(web, _
    solutionFileName, title, description, _
    exportMode, includeContent)
public static string ExportWebToGallery(
    SPWeb web,
    string solutionFileName,
    string title,
    string description,
    SPSolutionExporter.ExportMode exportMode,
    bool includeContent
)

Parameters

  • solutionFileName
    Type: System.String

    The name of the solution file (.wsp).

  • title
    Type: System.String

    The title of the Web template. The value passed in this parameter is used as the value of the Title attribute in the Project element of an Onet.xml file.

  • description
    Type: System.String

    Detailed information that describes the Web template. The value passed in this parameter is used as the value of the Description attribute in the WebTemplate element in an element manifest and also for the value of the Description attribute in the Project element of an Onet.xml file.

    Users see the description when they choose New Site from the Site Actions menu and then select the template.

  • exportMode
    Type: Microsoft.SharePoint.SPSolutionExporter.ExportMode

    Specifies how much of the Web site to export. Pass FullReuse if you intend to use the Web template within the same site collection as the exported Web site; otherwise, pass FullPortability.

  • includeContent
    Type: System.Boolean

    true to include the contents of all lists and document libraries in the Web site; otherwise false.

Return Value

Type: System.String
A string containing the URL of the new solution file in the Solution Gallery. If a solution could not be created, an empty string is returned.

Remarks

This method attempts to copy the new solution file into the Solution Gallery using the specified file name. If a file with this name already exists, then a series of "FileName-2.wsp", "FileName-3.wsp" attempts are made in an effort to find a unique file name. If "FileName-1000.wsp" is reached and a file with that name already exists in the gallery, the method throws an InvalidOperationException exception.

The method creates a solution that contains some or all of the Features listed in the following table. Features are omitted if the Web site does not contain the elements that they provision.

Feature

Scope

Includes

Web Template

Site

  • An element manifest that contains a WebTemplate element.

  • An Onet.xml file that contains the Web site configuration.

  • A language resource file for the default language of the Web site. The file contains strings exported from SPUserResource objects such as the TitleResource that backs the display text for the title of the Web site.

List Instances

Web

  • One element manifest for each list or document library in the Web site. Each manifest contains a ListInstance element that defines the instance.

  • A Schema.xml file for each list or library.

  • A single element manifest that contains Field elements that define all fields used on lists and libraries.

  • A single element manifest that contains ContentType elements that define all content types used on lists and libraries.

  • A language resource file for the default language of the Web site.

Modules

Web

  • One or more element manifests with a Module element that provisions forms, custom pages, document templates, and other files used by the Web site.

  • A language resource file for the default language of the Web site.

Property Bag

Web

  • A single element manifest that contains a number of PropertyBag elements that define configuration data and other persisted settings used on the Web site.

  • A language resource file for the default language of the Web site.

Workflows

Web

  • An element manifest that contains WorkflowAssociation elements that define workflow associations on lists in the Web site.

  • A language resource file for the default language of the Web site.

Custom Actions

Web

An element manifest that contains CustomAction elements that define Web-scoped and list-scoped custom actions.

Examples

The following example is a console application that gets a reference to a Web site and calls the ExportWebToGallery method to save the Web site as a Web template solution in the Solution Gallery. After exporting the solution, the application calls the GetAvailableWebTemplates method to retrieve the new template and prints its title to the console.

using System;
using System.IO;
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.OpenWeb("Child"))
                {
                    string solutionFileName = "Test Site.wsp"; // If this name exists, a number is appended.
                    string templateTitle = String.Format("{0} Web Template", web.Title);
                    string templateDesc = "This template was saved programmatically.";

                    // Save the web as a solution in the Solutions Gallery.
                    string solutionFileUrl = SPSolutionExporter.ExportWebToGallery(
                        web,
                        solutionFileName,
                        templateTitle,
                        templateDesc,
                        SPSolutionExporter.ExportMode.FullReuse, 
                        false // Do not include content.
                        );

                     if (!String.IsNullOrEmpty(solutionFileUrl))
                    {
                         // The solution file name might have changed, so get it from the Url.
                        string newFileName = Path.GetFileNameWithoutExtension(solutionFileUrl);

                        // Get the Web template.
                        SPWebTemplate template = web
                            .GetAvailableWebTemplates(web.Language)
                            .Cast<SPWebTemplate>()
                            .FirstOrDefault(t => t.Title == newFileName);

                        if (template != null)
                        {
                            // Print information about the Web template.
                            Console.WriteLine("Template Title: {0}", template.Title);
                        }
                    }
                }
            }
            Console.Write("\nPress ENTER to continue....");
            Console.ReadLine();
        }
    }
}
Imports System
Imports System.IO
Imports Microsoft.SharePoint

Module ConsoleApp

    Sub Main()

        Using site As New SPSite("https://localhost")
            Using web As SPWeb = site.OpenWeb("Child")

                ' If this name exists, a number is appended.
                Dim solutionFileName As String = "Test Site.wsp"

                Dim templateTitle As String = String.Format("{0} Web Template", web.Title)
                Dim templateDesc As String = "This template was saved programmatically."

                ' Save the web as a solution in the Solutions Gallery.
                Dim solutionFileUrl As String = SPSolutionExporter.ExportWebToGallery(web, _
                                                                                      solutionFileName, _
                                                                                      templateTitle, _
                                                                                      templateDesc, _
                                                                                      SPSolutionExporter.ExportMode.FullReuse, _
                                                                                      False)
                If Not String.IsNullOrEmpty(solutionFileUrl) Then
                    ' The solution file name might have changed, so get it from the Url.
                    Dim newFileName As String = Path.GetFileNameWithoutExtension(solutionFileUrl)

                    ' Get the Web template.
                    Dim template As SPWebTemplate = _
                        web.GetAvailableWebTemplates(web.Language).Cast(Of SPWebTemplate)().FirstOrDefault(Function(t) t.Title = newFileName)

                    If template IsNot Nothing Then
                        ' Print information about the Web template.
                        Console.WriteLine("Template Title: {0}", template.Title)
                    End If
                End If

            End Using
        End Using

        Console.Write(vbCrLf & "Press ENTER to continue....")
        Console.Read()
    End Sub

End Module

See Also

Reference

SPSolutionExporter Class

SPSolutionExporter Members

Microsoft.SharePoint Namespace

SaveAsTemplate

Other Resources

Site Types: WebTemplates and Site Definitions

Web Templates