如何:自定义已中断的方案的部署

上次修改时间: 2010年9月22日

适用范围: SharePoint Server 2010

当源场和目标场之间的明文连接总是可用时,内容部署将正常运行。但是,源创作场和目标生产场之间的可靠的功能连接并不总是可用。例如,当存在地理障碍、阻止访问的防火墙或网络中断时,使用网络连接将导出包传输到目标生产场是不可靠的。在这些情况下,Microsoft 建议您使用 Microsoft SharePoint Server 2010 对象模型和 SharePoint Foundation 内容迁移 API 以编程方式完成内容迁移的导出和导入步骤,并找出用于将导出包传输到目标生产场的替代方式,然后运行自定义导入代码。

部署服务器之间的内容中所述,每个内容部署都包含三个步骤:导出、传输和导入。在此方案中,通过手动而不是以编程方式处理传输。例如,可能通过便携媒体传输导出的内容包或使用 FTP 客户端将数据上载到可从其中导入的单独的文件共享。

使用内容迁移 API 导出内容包

  1. 调用带有要指定的选项的内容迁移 API。

  2. 存储更改标记。

  3. 应用程序将运行导出、检查异常并根据需要报告异常。如果运行成功,则应用程序将创建带有提供的名称的导出包并将其存储到提供的位置。

备注

在导出完成并创建内容包之后,可以使用 FTP、便携媒体等来传输已导出的包。在将数据传输到导入过程可以访问的文件位置之后,可以开始导入。

使用内容迁移 API 导入内容包

  1. 在导出过程中使用对象模型查找创建的文件。如果正在使用提供的代码示例,则文件名为 export.cmp。

  2. 运行导入代码。导入代码将执行以下操作:

    1. 使用代码中定义的属性集来创建 SPImportSettings 对象。

    2. 创建新的 SPImport 对象并管理预先导入和导入后更改标记、内容版本控制、缓存设置和针对新部署的内容的安排。

    备注

    由于性能方面的原因,默认情况下,SharePoint Server 2010 不调用事件接收器。默认情况下,当内容部署创建内容时不会触发侦听器。但是,如果使用的是第三方应用程序或要求在导入过程中触发事件接收器的自定义代码,则可将 SuppressAfterEvents 属性设置为 false。在设置此标志之后,可能会遭受重大的性能损失,但是会接收到导入中指定的所有要求。

    备注

    在源服务器上调用内容迁移 API 并将特定选项(例如,RetainObjectIdentity 属性)设置为 true。

示例

为了帮助您将 SharePoint Foundation 内容迁移 API 与 SharePoint Server 2010 内容部署功能一起使用,本主题包含一个将内容迁移 API 与 SharePoint Server 2010 内容部署一起使用的代码示例。此示例演示如何将内容部署功能与内容迁移 API 进行交互以创建导出;在使用内容迁移 API 完成导出、导入和路径时所需的概念和语法;以及 SharePoint Server 2010 内容部署的作业定义和执行步骤。

示例代码划分为两个较小的应用程序:导出应用程序和导入应用程序。

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Deployment;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Publishing.Administration;

/* Note: Before running this sample code, or any custom deployment solution, disable auto-spawning for the Variations feature
 * on your source site if it is enabled.
 **/
namespace CustomDeployment
{
    class Program
    {
        private static SavedCacheSettings savedCacheSettings;
        private static string sourceUrl = "http://samplesource/";
        private static string destinationUrl = "http://sampledestination/";
        private static string destinationRootWebUrl;

        static void Main(string[] args)
        {
            // This region defines content deployment export settings,
            // runs the export, and catches an exceptions during export
            // and outputs them.
            #region Export

            try
            {
                SPExportSettings exportSettings = new SPExportSettings();
                // Turn on extra output for testing
                exportSettings.CommandLineVerbose = true; 
                // The name of the file targeted by the export
                exportSettings.BaseFileName = "export.cmp"; 
                // The directory on the file system in which
                // to put the exported package
                exportSettings.FileLocation = @"%temp%"; 
                // If the file exists, overwrite it
                exportSettings.OverwriteExistingDataFile = true;
                // Export all security settings; change this if needed
                exportSettings.IncludeSecurity = SPIncludeSecurity.All;
                // The URL of the site being exported
                exportSettings.SiteUrl = sourceUrl;
                // The last major and minor versions
                exportSettings.IncludeVersions = SPIncludeVersions.LastMajorAndMinor;
                // Compress the exported file
                exportSettings.FileCompression = true; 
                // Create a new export object with the correct settings
                SPExport export = new SPExport(exportSettings); 
                // Optionally add event handlers to the export
                // object for:
                    // Started
                    // ProgressUpdated
                    // Completed
                    // Canceled
                    // Error
                    // Compressing
                // Run the export
                   export.Run(); 
                   }
                // Catch any exceptions during export and output them
                   catch (Exception ex) 
                   {
                   Console.Error.Write(ex.ToString());
                   throw;
                }
            #endregion //Export

            // This region defines import settings, creates a new
            // SPImportObject with those settings, manages versioning
            // and caching, and applies appropriate conditions if the
            // object is a Web site or root Web site
            #region Import
            try
            {
                SPImportSettings importSettings = new SPImportSettings();
                // Turn on extra output for testing
                importSettings.CommandLineVerbose = true; 
                // IMPORTANT: retains object IDs
                importSettings.RetainObjectIdentity = true; 
                // The directory of the file being imported
                importSettings.FileLocation = @"%temp%"; 
                // The name of the file being imported
                importSettings.BaseFileName = "export.cmp";
                // The URL of the site into which content is being imported
                importSettings.SiteUrl = destinationUrl; 
                //Import all the security settings from the package
                importSettings.IncludeSecurity = SPIncludeSecurity.All;
                // Retain author name during import; change if needed
                importSettings.UserInfoDateTime = SPImportUserInfoDateTimeOption.ImportAll;
                // Don't fire event receivers during import; 
                // change if needed
                importSettings.SuppressAfterEvents = true;
                // Add new versions when importing items that 
                // already exist
                importSettings.UpdateVersions = SPUpdateVersions.Append; 
                // Create a new import object with specified settings
                SPImport import = new SPImport(importSettings); 
                // Turn down caching during the import
                  SiteCacheSettingsWriter.UpdateCacheSettingsDuringImport(importSettings.SiteUrl, true); 
                // Save the cache settings to restore after 
                // the import finishes
                savedCacheSettings = SiteCacheSettingsWriter.SaveCacheSettingsBeforeImport(importSettings.SiteUrl);
                // Change tokens for pre-import and post-import
                SPChangeToken startChangeToken, endChangeToken; 
                using (SPSite destinationSite = new SPSite(importSettings.SiteUrl))
                {
                    // Get the change token from the destination site
                    startChangeToken = destinationSite.CurrentChangeToken; 
                    // Save the root Web URL for future use
                    destinationRootWebUrl = destinationSite.RootWeb.ServerRelativeUrl; 
                }

                import.ObjectImported += new EventHandler<SPObjectImportedEventArgs>(import_ObjectImported);
                // Optionally add event handlers to the 
                // import object for:
                    // Started
                    // ProgressUpdated
                    // Completed
                    // Cancelled
                    // Error
                    // Uncompressing

                // Run the import
                import.Run();

                using (SPSite destinationSite = new SPSite(importSettings.SiteUrl))
                {
                    // Get the change token from the
                    // destination site AFTER import
                    endChangeToken = destinationSite.CurrentChangeToken; 

                    // Enable scheduling of the items just deployed
                    ScheduledItem.EnableSchedulingOnDeployedItems(destinationSite, startChangeToken, endChangeToken, "Succeeded");
                }
            }
            // Catch any exceptions during import and output them
            catch (Exception ex) 
            {
                Console.Error.Write(ex.ToString());
                throw;
            }
            finally
            {
                // Update the cache settings because import is done
                SiteCacheSettingsWriter.UpdateCacheSettingsDuringImport(destinationUrl, false);
            }
            #endregion
        }

        private static void import_ObjectImported(object sender, SPObjectImportedEventArgs e)
        {
            // Is the imported object a Web site?
            if ((e != null) && (e.Type == SPDeploymentObjectType.Web))
            {
                // Is the imported object the root Web site?
                if (string.Compare(e.TargetUrl, destinationRootWebUrl, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    // The root Web site is being imported, so restore
                    // the cache-related root Web site properties.
                    SiteCacheSettingsWriter.RestoreCacheSettingsOnImport(destinationUrl, savedCacheSettings);
                }
            }

            return;
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.SharePoint.Deployment
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Publishing
Imports Microsoft.SharePoint.Publishing.Administration

' Note: Before running this sample code, or any custom deployment solution, disable auto-spawning for the Variations feature
' * on your source site if it is enabled.
' *
Namespace CustomDeployment
    Friend Class Program
        Private Shared savedCacheSettings As SavedCacheSettings
        Private Shared sourceUrl As String = "http://samplesource/"
        Private Shared destinationUrl As String = "http://sampledestination/"
        Private Shared destinationRootWebUrl As String

        Shared Sub Main(ByVal args() As String)
            ' This region defines content deployment export settings,
            ' runs the export, and catches an exceptions during export
            ' and outputs them.
'            #Region "Export"

            Try
                Dim exportSettings As New SPExportSettings()
                ' Turn on extra output for testing
                exportSettings.CommandLineVerbose = True
                ' The name of the file targeted by the export
                exportSettings.BaseFileName = "export.cmp"
                ' The directory on the file system in which
                ' to put the exported package
                exportSettings.FileLocation = "%temp%"
                ' If the file exists, overwrite it
                exportSettings.OverwriteExistingDataFile = True
                ' Export all security settings; change this if needed
                exportSettings.IncludeSecurity = SPIncludeSecurity.All
                ' The URL of the site being exported
                exportSettings.SiteUrl = sourceUrl
                ' The last major and minor versions
                exportSettings.IncludeVersions = SPIncludeVersions.LastMajorAndMinor
                ' Compress the exported file
                exportSettings.FileCompression = True
                ' Create a new export object with the correct settings
                Dim export As New SPExport(exportSettings)
                ' Optionally add event handlers to the export
                ' object for:
                    ' Started
                    ' ProgressUpdated
                    ' Completed
                    ' Canceled
                    ' Error
                    ' Compressing
                ' Run the export
                   export.Run()
                ' Catch any exceptions during export and output them
                   Catch ex As Exception
                   Console.Error.Write(ex.ToString())
                   Throw
                   End Try
'            #End Region 'Export

            ' This region defines import settings, creates a new
            ' SPImportObject with those settings, manages versioning
            ' and caching, and applies appropriate conditions if the
            ' object is a Web site or root Web site
'            #Region "Import"
            Try
                Dim importSettings As New SPImportSettings()
                ' Turn on extra output for testing
                importSettings.CommandLineVerbose = True
                ' IMPORTANT: retains object IDs
                importSettings.RetainObjectIdentity = True
                ' The directory of the file being imported
                importSettings.FileLocation = "%temp%"
                ' The name of the file being imported
                importSettings.BaseFileName = "export.cmp"
                ' The URL of the site into which content is being imported
                importSettings.SiteUrl = destinationUrl
                'Import all the security settings from the package
                importSettings.IncludeSecurity = SPIncludeSecurity.All
                ' Retain author name during import; change if needed
                importSettings.UserInfoDateTime = SPImportUserInfoDateTimeOption.ImportAll
                ' Don't fire event receivers during import; 
                ' change if needed
                importSettings.SuppressAfterEvents = True
                ' Add new versions when importing items that 
                ' already exist
                importSettings.UpdateVersions = SPUpdateVersions.Append
                ' Create a new import object with specified settings
                Dim import As New SPImport(importSettings)
                ' Turn down caching during the import
                  SiteCacheSettingsWriter.UpdateCacheSettingsDuringImport(importSettings.SiteUrl, True)
                ' Save the cache settings to restore after 
                ' the import finishes
                savedCacheSettings = SiteCacheSettingsWriter.SaveCacheSettingsBeforeImport(importSettings.SiteUrl)
                ' Change tokens for pre-import and post-import
                Dim startChangeToken, endChangeToken As SPChangeToken
                Using destinationSite As New SPSite(importSettings.SiteUrl)
                    ' Get the change token from the destination site
                    startChangeToken = destinationSite.CurrentChangeToken
                    ' Save the root Web URL for future use
                    destinationRootWebUrl = destinationSite.RootWeb.ServerRelativeUrl
                End Using

                AddHandler import.ObjectImported, AddressOf import_ObjectImported
                ' Optionally add event handlers to the 
                ' import object for:
                    ' Started
                    ' ProgressUpdated
                    ' Completed
                    ' Cancelled
                    ' Error
                    ' Uncompressing

                ' Run the import
                import.Run()

                Using destinationSite As New SPSite(importSettings.SiteUrl)
                    ' Get the change token from the
                    ' destination site AFTER import
                    endChangeToken = destinationSite.CurrentChangeToken

                    ' Enable scheduling of the items just deployed
                    ScheduledItem.EnableSchedulingOnDeployedItems(destinationSite, startChangeToken, endChangeToken, "Succeeded")
                End Using
            ' Catch any exceptions during import and output them
            Catch ex As Exception
                Console.Error.Write(ex.ToString())
                Throw
            Finally
                ' Update the cache settings because import is done
                SiteCacheSettingsWriter.UpdateCacheSettingsDuringImport(destinationUrl, False)
            End Try
'            #End Region
        End Sub

        Private Shared Sub import_ObjectImported(ByVal sender As Object, ByVal e As SPObjectImportedEventArgs)
            ' Is the imported object a Web site?
            If (e IsNot Nothing) AndAlso (e.Type = SPDeploymentObjectType.Web) Then
                ' Is the imported object the root Web site?
                If String.Compare(e.TargetUrl, destinationRootWebUrl, StringComparison.OrdinalIgnoreCase) = 0 Then
                    ' The root Web site is being imported, so restore
                    ' the cache-related root Web site properties.
                    SiteCacheSettingsWriter.RestoreCacheSettingsOnImport(destinationUrl, savedCacheSettings)
                End If
            End If

            Return
        End Sub
    End Class
End Namespace

请参阅

任务

如何:部署服务器之间的内容

引用

Web

概念

部署服务器之间的内容