How to create a replica in a disconnected environment


Summary
This topic shows how to create a replica in a disconnected environment using ArcObjects. A disconnected environment is one in which the parent and child replica geodatabases are not connected by a local area network (LAN) or a wide area network (WAN).


Exporting the replica workspace document from the parent replica geodatabase

Complete the following steps to export the replica workspace document:
  1. Connect to a geodatabase to host the parent replica. This requires initializing a GeoDataServer object. GeoDataServer objects allow access to a geodatabase on a LAN or WAN through ArcGIS for Server. For more information on how to initialize a GeoDataServer, see How to initialize a GeoDataServer object.
  2. Call the following procedure by pasting it into an application and passing in the following parameters:
Parameter
Description
parentGDS
The GeoDataServer that initialized in Step 1.  This GeoDataServer hosts the parent replica.
replicaName
The string name of the replica to be created.
accessType
This parameter specifies the type of replica to create. Three replica types are supported: checkout replicas, two-way replicas, and one-way replicas. Two-way and one-way replicas require an ArcSDE geodatabase for the child replica. However, checkout replicas allow personal, file, or ArcSDE geodatabases for the child replica. The parent replica must always be hosted by an ArcSDE geodatabase.
featureDataset
This parameter is the name of the feature dataset to replicate. All datasets to be replicated must be registered as versioned on the parent replica's geodatabase. Two-way and one-way replicas also require the datasets to have a GlobalID column and be stored with a high-precision coordinate system.
geometry
This parameter specifies the replica geometry that is applied as a filter where only features intersecting the geometry are replicated. If the filter is set to null or nothing, all features are replicated.
registerOnly
False means that the child does not have the data. True means that the data exists in both geodatabases, and new replicas should be registered for this data. In typical cases, set this to false.
useArchiving
True means that archiving will be used to track changes for one-way replication. This capability was added at ArcGIS 10. False means that versioning will be used to track changes for one-way replication.
outputDirectory
This parameter specifies the output location for the replica workspace document. This location should point to a directory on a local drive. If this folder exists, it will be replaced. If it does not exist, it will be created. The replica workspace document created by this function is written in a compressed Zip file format. This Zip file will be written to the sOutputDirectory passed to the function.

See the following code example:
[C#]
public void CreateDisconnectedReplica(IGeoDataServer parentGDS, String replicaName,
    esriReplicaAccessType accessType, String featureDataset, IGeometry geometry,
    Boolean registerOnly, Boolean useArchiving, String outputDirectory)
{
    try
    {
        // Set and expand the replica datasets.
        IGPReplicaDatasets gpReplicaDatasets = new GPReplicaDatasets();
        IGPReplicaDataset gpReplicaDataset = new GPReplicaDataset();
        gpReplicaDataset.DatasetType = esriDatasetType.esriDTFeatureDataset;
        gpReplicaDataset.Name = featureDataset;
        gpReplicaDatasets.Add(gpReplicaDataset);
        IGPReplicaDatasets gpReplicaDatasetsExpand = parentGDS.ExpandReplicaDatasets
            (gpReplicaDatasets);

        // Set the replica description.
        IGPReplicaDescription gpReplicaDesc = new GPReplicaDescription();
        gpReplicaDesc.ReplicaDatasets = gpReplicaDatasetsExpand;
        gpReplicaDesc.ModelType = esriReplicaModelType.esriModelTypeFullGeodatabase;
        gpReplicaDesc.SingleGeneration = (accessType ==
            esriReplicaAccessType.esriReplicaAccessNone);
        gpReplicaDesc.QueryGeometry = geometry;

        // Set the replica options.
        IGPReplicaOptions2 replicaOptions = new GPReplicaOptions();
        replicaOptions.AccessType = accessType;
        replicaOptions.ChildReconcilePolicy =
            esriReplicaReconcilePolicyType.esriReplicaResolveConflictsNone;
        replicaOptions.ParentReconcilePolicy =
            esriReplicaReconcilePolicyType.esriReplicaResolveConflictsNone;
        replicaOptions.IsChildFirstSender = true;
        replicaOptions.RegisterReplicaOnly = registerOnly;
        replicaOptions.UseArchiving = useArchiving;

        // Set the export options.
        IGDSExportOptions gdsExportOptions = new GDSExportOptions();
        gdsExportOptions.ExportFormat = esriGDSExportFormat.esriGDSExportFormatXml;
        gdsExportOptions.Compressed = true;
        gdsExportOptions.BinaryGeometry = false;

        // Create the replica.
        IGDSData gdsData = parentGDS.CreateReplica("", replicaName, gpReplicaDesc,
            replicaOptions, gdsExportOptions,
            esriGDSTransportType.esriGDSTransportTypeUrl);

        // Force deletion of folder and contents if they exist.
        if (Directory.Exists(outputDirectory))
        {
            Directory.Delete(outputDirectory, true);
        }

        // Create the output folder.
        Directory.CreateDirectory(outputDirectory);

        // Get the compressed replica workspace document file from the uniform resource locator
        //  (URL) to the local output directory.
        if (gdsData.TransportType == esriGDSTransportType.esriGDSTransportTypeUrl)
        {
            String fileName = System.IO.Path.GetFileName(gdsData.URL);
            String outputFileName = System.IO.Path.Combine(outputDirectory, fileName)
                ;
            WebClient webClient = new WebClient();
            webClient.DownloadFile(gdsData.URL, outputFileName);
            webClient.Dispose();
        }
        else
        {
            // The file has been embedded because there is no output directory set on ArcGIS Server.
            Console.WriteLine("Server is not configured with a virtual directory.");
        }
    }
    catch (COMException comExc)
    {
        throw new Exception(String.Format(
            "Create replica errored: {0}, Error Code: {1}", comExc.Message,
            comExc.ErrorCode), comExc);
    }
    catch (Exception exc)
    {
        throw new Exception(String.Format("Create replica errored: {0}", exc.Message)
            , exc);
    }
}
[VB.NET]
Public Sub CreateDisconnectedReplica(ByVal parentGDS As IGeoDataServer, ByVal replicaName As String, ByVal accessType As esriReplicaAccessType, ByVal featureDataset As String, ByVal geometry As IGeometry, ByVal registerOnly As Boolean, ByVal useArchiving As Boolean, ByVal outputDirectory As String)
    
    Try
    ' Set and expand the replica datasets.
    Dim gpReplicaDatasets As IGPReplicaDatasets = New GPReplicaDatasets()
    Dim gpReplicaDataset As IGPReplicaDataset = New GPReplicaDataset()
    gpReplicaDataset.DatasetType = esriDatasetType.esriDTFeatureDataset
    gpReplicaDataset.Name = featureDataset
    gpReplicaDatasets.Add(gpReplicaDataset)
    Dim gpReplicaDatasetsExpand As IGPReplicaDatasets = parentGDS.ExpandReplicaDatasets(gpReplicaDatasets)
    
    ' Set the replica description.
    Dim gpReplicaDesc As IGPReplicaDescription = New GPReplicaDescription()
    gpReplicaDesc.ReplicaDatasets = gpReplicaDatasetsExpand
    gpReplicaDesc.ModelType = esriReplicaModelType.esriModelTypeFullGeodatabase
    Dim singleGeneration As Boolean
    If accessType = esriReplicaAccessType.esriReplicaAccessNone Then
        singleGeneration = True
    Else
        singleGeneration = False
    End If
    gpReplicaDesc.SingleGeneration = singleGeneration
    gpReplicaDesc.QueryGeometry = geometry
    
    ' Set the replica options.
    Dim replicaOptions As IGPReplicaOptions2 = New GPReplicaOptions()
    replicaOptions.AccessType = accessType
    replicaOptions.ChildReconcilePolicy = esriReplicaReconcilePolicyType.esriReplicaResolveConflictsNone
    replicaOptions.ParentReconcilePolicy = esriReplicaReconcilePolicyType.esriReplicaResolveConflictsNone
    replicaOptions.IsChildFirstSender = True
    replicaOptions.RegisterReplicaOnly = registerOnly
    replicaOptions.UseArchiving = useArchiving
    
    ' Set the export options.
    Dim gdsExportOptions As IGDSExportOptions = New GDSExportOptions()
    gdsExportOptions.ExportFormat = esriGDSExportFormat.esriGDSExportFormatXml
    gdsExportOptions.Compressed = True
    gdsExportOptions.BinaryGeometry = False
    
    ' Create the replica.
    Dim gdsData As IGDSData = parentGDS.CreateReplica("", replicaName, gpReplicaDesc, replicaOptions, gdsExportOptions, esriGDSTransportType.esriGDSTransportTypeUrl)
    
    ' Force deletion of folder and contents if they exist.
    If Directory.Exists(outputDirectory) Then
        Directory.Delete(outputDirectory, True)
    End If
    
    ' Create the output folder.
    Directory.CreateDirectory(outputDirectory)
    
    ' Get the compressed replica workspace document file from the uniform resource locator
    ' (URL) to the local output directory.
    If gdsData.TransportType = esriGDSTransportType.esriGDSTransportTypeUrl Then
        Dim fileName As String = System.IO.Path.GetFileName(gdsData.URL)
        Dim outputFileName As String = System.IO.Path.Combine(outputDirectory, fileName)
        Dim webClient As WebClient = New WebClient()
        webClient.DownloadFile(gdsData.URL, outputFileName)
        webClient.Dispose()
    Else
        ' The file has been embedded because there is no output directory set on ArcGIS Server.
        Console.WriteLine("Server is not configured with a virtual directory.")
    End If
    Catch comExc As COMException
    Throw New Exception(String.Format("Create replica errored: {0}, Error Code: {1}", comExc.Message, comExc.ErrorCode), comExc)
    Catch exc As Exception
    Throw New Exception(String.Format("Create replica errored: {0}", exc.Message), exc)
    End Try
    
End Sub

Importing the replica workspace document into the child replica geodatabase

Complete the following steps to import the replica workspace document:
  1. Connect to a geodatabase to host the child replica. This requires initializing a GeoDataServer object. GeoDataServer objects allow access to a geodatabase on a LAN or WAN through ArcGIS for Server. 
  2. Call the following procedure by pasting it into an application and passing in the following parameters:
Parameter
Description
childGDS
This is the GeoDataServer initialized in Step 1. This GeoDataServer will host the child replica.
inputDirectory
This specifies the input location for the replica workspace document. This location should point to the directory on a local drive that holds the replica workspace document. The function expects that the replica workspace document is written in a compressed format (Extensible Markup Language [XML] compressed as a Zip file). See the following note.
The Zip file must have the same name as the XML file contained within it. The function can easily be modified to import uncompressed XML by setting the gdsData.Compressed property to false. The function also assumes that there is only one file in this directory. If there is more than one file in the inputDirectory, only the first file will be read.
See the following code example:
[C#]
public void ImportReplica(IGeoDataServer childGDS, String inputDirectory)
{
    try
    {
        // Make sure the input directory exists.
        if (!Directory.Exists(inputDirectory))
        {
            throw new DirectoryNotFoundException(String.Format(
                "Input directory could not be found: {0}", inputDirectory));
        }

        // Get the first file from the input directory.
        DirectoryInfo directoryInfo = new DirectoryInfo(inputDirectory);
        FileInfo[] fileInfoArray = directoryInfo.GetFiles();
        if (fileInfoArray.Length == 0)
        {
            throw new FileNotFoundException("No input files could be found.");
        }
        String inputFile = System.IO.Path.Combine(inputDirectory,
            fileInfoArray[0].Name);

        // Read the .xml file into a byte array to pass to the gdsData object.
        FileInfo fileInfo = new FileInfo(inputFile);
        long fileLength = fileInfo.Length;
        byte[] fileBytes = new Byte[fileLength];

        System.IO.FileStream fileStream = File.Open(inputFile, FileMode.Open,
            FileAccess.Read);
        BinaryReader binaryReader = new BinaryReader(fileStream);
        binaryReader.Read(fileBytes, 0, (int)fileLength);
        binaryReader.Close();
        fileStream.Close();

        // Embed the GDSData object.
        IGDSData gdsData = new GDSDataClass();
        gdsData.TransportType = esriGDSTransportType.esriGDSTransportTypeEmbedded;
        gdsData.Compressed = true;
        gdsData.set_EmbeddedData(ref fileBytes);

        // Import the replica workspace document to create the replica.
        childGDS.ImportData(gdsData,
            esriGDSImportFormat.esriGDSImportFormatXmlWorkspace);
    }
    catch (COMException comExc)
    {
        throw new Exception(String.Format(
            "Create replica errored: {0}, Error Code: {1}", comExc.Message,
            comExc.ErrorCode), comExc);
    }
    catch (Exception exc)
    {
        throw new Exception(String.Format("Create replica errored: {0}", exc.Message)
            , exc);
    }
}
[VB.NET]
Public Sub ImportReplica(ByVal childGDS As IGeoDataServer, ByVal inputDirectory As String)
    
    Try
    ' Make sure the input directory exists.
    If (Not Directory.Exists(inputDirectory)) Then
        Throw New DirectoryNotFoundException(String.Format("Input directory could not be found: {0}", inputDirectory))
    End If
    
    ' Get the first file from the input directory.
    Dim directoryInfo As DirectoryInfo = New DirectoryInfo(inputDirectory)
    Dim fileInfoArray As FileInfo() = directoryInfo.GetFiles()
    If fileInfoArray.Length = 0 Then
        Throw New FileNotFoundException("No input files could be found.")
    End If
    Dim inputFile As String = System.IO.Path.Combine(inputDirectory, fileInfoArray(0).Name)
    
    ' Read the .xml file into a byte array to pass to the gdsData object.
    Dim fileInfo As FileInfo = New FileInfo(inputFile)
    Dim fileLength As Long = fileInfo.Length
    Dim fileBytes As Byte() = New Byte(CInt(fileLength - 1)) {}
    Dim fileStream As System.IO.FileStream = File.Open(inputFile, FileMode.Open, FileAccess.Read)
    Dim binaryReader As BinaryReader = New BinaryReader(fileStream)
    binaryReader.Read(fileBytes, 0, CInt(Fix(fileLength)))
    binaryReader.Close()
    fileStream.Close()
    
    ' Embed the GDSData object.
    Dim gdsData As IGDSData = New GDSDataClass()
    gdsData.TransportType = esriGDSTransportType.esriGDSTransportTypeEmbedded
    gdsData.Compressed = True
    gdsData.EmbeddedData = fileBytes
    
    ' Import the replica workspace document to create the replica.
    childGDS.ImportData(gdsData, esriGDSImportFormat.esriGDSImportFormatXmlWorkspace)
    Catch comExc As COMException
    Throw New Exception(String.Format("Create replica errored: {0}, Error Code: {1}", comExc.Message, comExc.ErrorCode), comExc)
    Catch exc As Exception
    Throw New Exception(String.Format("Create replica errored: {0}", exc.Message), exc)
    End Try
    
End Sub






To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
Engine Developer Kit Engine: Geodatabase Update