How to create a replica in a disconnected environment


This sample demonstrates how to create a replica in a disconnected environment. A disconnected environment means that the geodatabases involved are not connected by a computer network.
In a disconnected environment the creation process is completed in two separate steps:
Step 1 - Export the replica workspace document from the parent geodataserver.
Step 2 - Import the replica workspace document into the child geodataserver.
 
This sample includes two routines that demonstrate how to create a replica workspace document (XML) under different circumstances. One demonstrates connecting to the ArcGIS server via LAN and the other via WAN. Both routines generate a workspace document which written in compressed format (.ZIP). This .zip file will be located in an output directory specified in the routine. This location has been hard-coded and should be modified to point to a directory on your local drive.
 
This sample also includes two different routines that demonstrate how to import a replica workspace document under different circumstances. One demonstrates connecting via a LAN connection to the ArcGIS Server, and the other via WAN. The routines look to an input folder for an existing compressed replica workspace document. This path has been hard-coded and should be modified to point to the directory on the local drive which contains the compressed replica workspace document.
 
The purpose of this sample is to provide some code that you can adjust to suit your needs.

How to use

  1. Paste the code into VBA in ArcMap or ArcCatalog. The code can also be associated with a command click events in Visual Basic.
  2. Find the routine that performs the type of replica creation needed. To create a replica workspace document you will need to use either Create_2wayReplica_ToXML_LAN() or Create_2wayReplica_ToXML_WAN(). To import the replica workspace document on the child Geodataserver you will need to use either Import_2wayReplica_FromXML_LAN() or Import_2wayReplica_FromXML_LAN()
  3. Adjust the code appropriately and run the routine. The code below has hard coded server connections, input and output directories, dataset names and replica names.
[VBA]
'++ Creates a 2 way replica to a zipped XML file. Remote GeoDataServer.
'++ The ZIP file must then be imported to another GDS to complete the creation of the replica
'++ objects served by ArcGIS server over the WAN are used.

Sub Create_2wayReplica_ToXML_WAN()
    
    Dim pGDSparent As IGeoDataServer
    Dim bOK As Boolean
    Dim scf As IAGSServerConnectionFactory
    Dim cprops As IPropertySet
    Dim psc As IAGSServerConnection
    Dim pSons As IAGSEnumServerObjectName
    Dim pSon As IAGSServerObjectName
    Dim pName As IName
    Dim pGDSData As IGDSData
    Dim sOutputDirectory As String, sOutputfile As String
    Dim pReplicaOptions As IGPReplicaOptions
    Dim pGPReplicaDesc As IGPReplicaDescription
    Dim pGPReplicaDatasets As IGPReplicaDatasets
    Dim pGPReplicaDatasets_Expand As IGPReplicaDatasets
    Dim pGPReplicaDataset As IGPReplicaDataset
    Dim pGDSExportOptions As IGDSExportOptions
    Dim pRequest As WinHttpRequest
    Dim pByte() As Byte
    Dim lFileHandle As Long
    Dim pFSObject As Object
    
    '++ Get the server objects published on the the ArcGIS server named bobmk
    Set cprops = New PropertySet
    cprops.SetProperty "Machine", "bobmk"
    Set scf = New AGSServerConnectionFactory
    Set psc = scf.Open(cprops, 0)
    Set pSons = psc.ServerObjectNames
    pSons.Reset
    
    '++ Get the parent server object
    Set pSon = pSons.Next
    Do Until pSon.Name = "bob_5165"
        Set pSon = pSons.Next
    Loop
    Set pName = pSon
    Set pGDSparent = pName.Open
    
    '++ Set the output directory where the file will be written
    '++ If this folder exists, it will be replaced. If it doesn't exist, it will be created.
    sOutputDirectory = "C:\temp\CreateWAN\" '++ Set the output directory where the file will be written
    
    '++ Create the replica description. In this sample, we are replicating all
    Set pGPReplicaDesc = New GPReplicaDescription
    Set pGPReplicaDatasets = New GPReplicaDatasets
    Set pGPReplicaDataset = New GPReplicaDataset
    With pGPReplicaDataset
        .datasetType = esriDTFeatureDataset
        .Name = "SampleDataset"
    End With
    pGPReplicaDatasets.Add pGPReplicaDataset
    Set pGPReplicaDatasets_Expand = pGDSparent.ExpandReplicaDatasets(pGPReplicaDatasets)
    With pGPReplicaDesc
        Set .ReplicaDatasets = pGPReplicaDatasets_Expand
        .ModelType = esriModelTypeFullGeodatabase
        .SingleGeneration = False
    End With
    
    '++ Set the replica options
    Set pReplicaOptions = New GPReplicaOptions
    With pReplicaOptions
        .AccessType = esriReplicaBothReadWrite
        .ChildReconcilePolicy = esriReplicaResolveConflictsNone
        .ParentReconcilePolicy = esriReplicaResolveConflictsNone
        .IsChildFirstSender = True
    End With
    
    '++ Set the Export Options
    Set pGDSExportOptions = New GDSExportOptions
    pGDSExportOptions.ExportFormat = esriGDSExportFormatXml
    pGDSExportOptions.Compressed = True
    pGDSExportOptions.BinaryGeometry = False
    
    Set pGDSData = pGDSparent.CreateReplica("", "Sample_Create2way_toXML_Wan", pGPReplicaDesc, pReplicaOptions, pGDSExportOptions, esriGDSTransportTypeUrl)
    
    '++ Replace or Create the output directory
    '++ If directory already exists - delete it and recreate by copying
    '++ Force deletion of folder and contents if it exists
    Set pFSObject = CreateObject("Scripting.FileSystemObject")
    If pFSObject.FolderExists(sOutputDirectory) Then
        pFSObject.DeleteFolder sOutputDirectory, True
    End If
    
    pFSObject.CreateFolder sOutputDirectory
    
    '++ Get the compressed replica workspace document file from the URL to the local output directory
    If pGDSData.TransportType = esriGDSTransportTypeUrl Then
        '++ if there is an output directory on the ArcGIS Server then the transport type will be URL
        Set pRequest = New WinHttpRequest
        pRequest.Open "GET", pGDSData.URL, False
        pRequest.Send
        
        If pRequest.Status <> 200 Then
            Debug.Assert False
            'make sure the virtual dir has anonymous access turned on
            '(win authentication works for IE, but not for WinHttp as used here)
            Err.Raise vbObjectError + pRequest.Status, "InternetUtils", pRequest.StatusText + " (" & pRequest.Status & ")"
        End If
        
        pByte() = pRequest.ResponseBody
        
        lFileHandle = FreeFile
        sOutputfile = sOutputDirectory & pFSObject.GetFileName(pGDSData.URL)
        Open sOutputfile For Binary Access Write As #lFileHandle
        Put #lFileHandle, , pByte
        Close fileHandle
    Else '++ the file has been embedded b/c there is no output directory set on the ArcGIS Server
        MsgBox "The ArcGIS server has not been configured with an output directory" & _
            vbCrLf & "Replica workspace document was not created."
        
    End If
    
    MsgBox "Created replica successfully to a compressed replica workspace file: " & _
        vbCrLf & "File Location: " & sOutputDirectory
    
End Sub



'++ Creates a 2 way replica using ArcSDE geodatabase. The geodatabase is
'++ accessed over the LAN.
'++ The ZIP file must then be imported to another GDS to complete the creation of the replica

Sub Create_2wayReplica_ToXML_LAN()
    
    Dim pGDSparent As IGeoDataServer
    Dim pGDSparentInit As IGeoDataServerInit
    Dim bOK As Boolean
    Dim sOutputDirectory As String
    Dim pReplicaOptions As IGPReplicaOptions
    Dim pGPReplicaDesc As IGPReplicaDescription
    Dim pGPReplicaDatasets As IGPReplicaDatasets
    Dim pGPReplicaDatasets_Expand As IGPReplicaDatasets
    Dim pGPReplicaDataset As IGPReplicaDataset
    Dim pGDSExportOptions As IGDSExportOptions
    Dim pGDSData As IGDSData
    Dim pRequest As WinHttpRequest
    Dim pByte() As Byte
    Dim lFileHandle As Long
    
    '++ Initialize the parent server object
    Set pGDSparent = New GeoDataServer
    Set pGDSparentInit = pGDSparent
    pGDSparentInit.InitFromConnectionString "SERVER=bobmk;INSTANCE=5165;VERSION=sde.DEFAULT;USER=qa;PASSWORD=qa;DATABASE=qa1"
    
    '++ Create the replica
    sOutputDirectory = "C:\temp\CreateLAN"
    
    '++ Create the replica description. In this sample, we are replicating all
    Set pGPReplicaDesc = New GPReplicaDescription
    Set pGPReplicaDatasets = New GPReplicaDatasets
    Set pGPReplicaDataset = New GPReplicaDataset
    With pGPReplicaDataset
        .datasetType = esriDTFeatureDataset
        .Name = "SampleDataset"
    End With
    pGPReplicaDatasets.Add pGPReplicaDataset
    Set pGPReplicaDatasets_Expand = pGDSparent.ExpandReplicaDatasets(pGPReplicaDatasets)
    With pGPReplicaDesc
        Set .ReplicaDatasets = pGPReplicaDatasets_Expand
        .ModelType = esriModelTypeFullGeodatabase
        .SingleGeneration = False
    End With
    
    '++ Set the replica options
    Set pReplicaOptions = New GPReplicaOptions
    With pReplicaOptions
        .AccessType = esriReplicaBothReadWrite
        .ChildReconcilePolicy = esriReplicaResolveConflictsNone
        .ParentReconcilePolicy = esriReplicaResolveConflictsNone
        .IsChildFirstSender = True
    End With
    
    '++ Set the Export Options
    Set pGDSExportOptions = New GDSExportOptions
    pGDSExportOptions.ExportFormat = esriGDSExportFormatXml
    pGDSExportOptions.Compressed = True
    pGDSExportOptions.BinaryGeometry = False
    
    '++ Create the replica to XML
    '++ Write the output XML Replica Document to the output directory on the Server
    '++ If no output directory has been configured, the data will be embedded
    Set pGDSData = pGDSparent.CreateReplica("", "Sample_Create_2way_toXML_LAN", pGPReplicaDesc, pReplicaOptions, pGDSExportOptions, esriGDSTransportTypeUrl)
    
    '++ Replace or Create the Output Directory and place output file in that location
    Dim pFSObject As Object
    Set pFSObject = CreateObject("Scripting.FileSystemObject")
    '++ If directory already exists - delete it and recreate by copying
    'Force deletion of folder and contents if it exists
    If pFSObject.FolderExists(sOutputDirectory) Then
        pFSObject.DeleteFolder sOutputDirectory, True
    End If
    pFSObject.CreateFolder sOutputDirectory
    pFSObject.CopyFile pGDSData.URL, sOutputDirectory & "\"
    
    MsgBox "Created replica to a compressed replica workspace file: " & _
        vbCrLf & "File Location: " & sOutputDirectory
    
End Sub



Sub Import_2wayReplica_FromXML_WAN()
    On Error GoTo eh
    
    Dim pGDSchild As IGeoDataServer
    Dim bOK As Boolean
    Dim scf As IAGSServerConnectionFactory
    Dim cprops As IPropertySet
    Dim psc As IAGSServerConnection
    Dim pSons As IAGSEnumServerObjectName
    Dim pSon As IAGSServerObjectName
    Dim pName As IName
    Dim pGDSData As IGDSData
    Dim lArrayMaxIndex As Long
    Dim pFSObject As FileSystemObject
    
    '++ Get the server objects published on the the ArcGIS server named baza
    Set cprops = New PropertySet
    cprops.SetProperty "Machine", "baza"
    Set scf = New AGSServerConnectionFactory
    Set psc = scf.Open(cprops, 0)
    Set pSons = psc.ServerObjectNames
    pSons.Reset
    
    '++ Get the child server object
    Set pSon = pSons.Next
    Do Until pSon.Name = "bob_5166"
        Set pSon = pSons.Next
    Loop
    Set pName = pSon
    Set pGDSchild = pName.Open
    
    '++ Specify Directory where the replica workspace document is located
    sInputDirectory = "C:\temp\CreateWAN\"
    
    '++ Get the ZIP file in the specified input Directory
    Dim pFSO As Object 'FileSystemObject
    Dim pFiles As Object 'File
    Dim pFolder As Object
    Dim sName As String
    Set pFSO = CreateObject("Scripting.FileSystemObject")
    Set pFolder = pFSO.GetFolder(sInputDirectory)
    Set pFiles = pFolder.Files
    For Each pFile In pFiles '++ Get the only file in the directory
        sName = pFile.Name
    Next
    
    '++ Read .zip file into byte array to pass to the GDSData object
    lFileHandle = FreeFile
    sName = sInputDirectory & sName
    Open sName For Binary Access Read As #lFileHandle
    
    lFileLen = LOF(lFileHandle) ' returns byte count, 1-based
    lArrayMaxIndex = lFileLen - 1 ' array is 0-based
    ReDim bArray(lArrayMaxIndex)
    
    Get #lFileHandle, , bArray
    Close lFileHandle
    
    '++ Set the GDSData object
    Set pGDSData = New GDSData
    pGDSData.TransportType = esriGDSTransportTypeEmbedded
    pGDSData.Compressed = True
    pGDSData.EmbeddedData = bArray
    
    '++ Import the Replica Workspace document to Create the Replica
    pGDSchild.ImportData pGDSData, esriGDSImportFormatXMLWorkspace
    
    MsgBox "Successfully imported the replica from " & sInputDirectory & sName
    Exit Sub
eh:
    MsgBox "Importing the XML Replica document failed: " & Err.Description & " : error number: " & Err.Number
End Sub

Sub Import_2wayReplica_FromXML_LAN()
    On Error GoTo eh
    
    Dim pGDSchild As IGeoDataServer
    Dim pGDSchildInit As IGeoDataServerInit
    Dim bOK As Boolean
    Dim pGDSData As IGDSData
    Dim sInFile As String
    Dim pFSObject As FileSystemObject
    
    '++ Initialize the child server object
    Set pGDSchild = New GeoDataServer
    Set pGDSchildInit = pGDSchild
    pGDSchildInit.InitFromConnectionString "SERVER=bobmk;INSTANCE=5166;VERSION=sde.DEFAULT;USER=qa;PASSWORD=qa;DATABASE=qa2"
    
    '++ Embed .xml file into a byte array to pass to the GDSData object
    sInputDirectory = "C:\temp\CreateLAN\"
    
    '++ Get the ZIP file in the specified input Directory
    Dim pFSO As Object 'FileSystemObject
    Dim pFiles As Object 'File
    Dim pFolder As Object
    Dim sName As String
    Set pFSO = CreateObject("Scripting.FileSystemObject")
    Set pFolder = pFSO.GetFolder(sInputDirectory)
    Set pFiles = pFolder.Files
    For Each pFile In pFiles '++ Get the only file in the directory
        sName = pFile.Name
    Next
    
    '++ Get the name of the file to import
    
    sName = sInputDirectory & sName
    
    '++ Set the GDSData object
    Set pGDSData = New GDSData
    pGDSData.TransportType = esriGDSTransportTypeUrl
    pGDSData.Compressed = True
    pGDSData.URL = sName
    
    '++ Import the Replica Workspace document to Create the Replica
    pGDSchild.ImportData pGDSData, esriGDSImportFormatXMLWorkspace
    
    MsgBox "Successfully imported the replica from " & sInputDirectory & sName
    Exit Sub
eh:
    MsgBox "Importing the XML Replica document failed: " & Err.Description & " : error number: " & Err.Number
End Sub