切断されたシナリオで展開をカスタマイズする

最終更新日: 2010年9月22日

適用対象: SharePoint Server 2010

コンテンツ展開は、展開元ファームと展開先ファームとの間の良好な接続が常に利用可能な場合に適切に動作します。しかし、展開元の作成ファームと展開先の運用ファームとの間で機能している信頼性の高い接続が常に利用可能であるとは限りません。たとえば、地理的な障壁、アクセスを妨げるファイアウォール、ネットワークの停止などがある状況では、ネットワーク接続を使用してエクスポート パッケージを展開先の運用ファームにトランスポートするのは信頼性のある方法とは言えません。これらの状況では、Microsoft SharePoint Server 2010 オブジェクト モデルおよび SharePoint Foundation コンテンツ移行 API を操作して、コンテンツ移行のエクスポートおよびインポート手順をプログラムによって完了し、エクスポート パッケージを展開先の運用ファームにトランスポートするための代替方法を見つけ、カスタム インポート コードを実行することをお勧めします。

サーバー間のコンテンツを展開する」に説明されているように、すべてのコンテンツ展開は、エクスポート、トランスポート、およびインポートの 3 つの手順から構成されます。このシナリオで、トランスポートは、プログラムによってではなく手動で処理されます。たとえば、ポータブル メディアにエクスポートしたコンテンツ パッケージをトランスポートしたり、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 コンテンツ展開のジョブ定義と実行手順を示しています。

サンプル コードは、エクスポート アプリケーションとインポート アプリケーションの 2 つの小さなアプリケーションに分かれています。

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

概念

サーバー間のコンテンツを展開する