SPSolutionExporter.ExportWebToGallery method (SPWeb, String, String, String, SPSolutionExporter.ExportMode, Boolean)
Exports the specified Web site as a Web template in the Solution Gallery.
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
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
web
Type: Microsoft.SharePoint.SPWebThe Web site to export.
solutionFileName
Type: System.StringThe name of the solution file (.wsp).
title
Type: System.StringThe 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.StringDetailed 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.ExportModeSpecifies 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.Booleantrue 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 |
|
List Instances |
Web |
|
Modules |
Web |
|
Property Bag |
Web |
|
Workflows |
Web |
|
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
Microsoft.SharePoint namespace