How to create a replica


This sample demonstrates how to create a replica in a connected environment. A connected environment means that the geodatabases involved are on the same network. This may be a LAN or a WAN if using ArcGIS server.
The sample includes a number of routines that demonstrate how to create a replica under different circumstances. For example, one routine creates a check-out replica between a local ArcSDE server and a personal geodatabase. Another creates a 2 way replica between two ArcSDE servers over a WAN using ArcGIS server. In each case the same generic function is called to create the replica.
The purpose of this sample is to provide some code that you can adjust to suit your needs. For example to create a 1 way replica using ArcSDE geodatabases on the LAN you'll need to adjust the Create_2wayReplica_LAN routine. Also the data to be replicated and the replica names are hard coded and need to be adjusted.

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.
  3. Adjust the code appropriately and run the routine. The code below has hard coded server connections, dataset names and replica names.
[VBA]
'++ Creates a check-out replica using an ArcSDE geodatabase and a personal geodatabase.
'++ The geodatabases are accessed over the LAN.

Sub Create_Checkout_ArcSDE_PGDB()
    
    Dim pGDSparent As IGeoDataServer
    Dim pGDSparentInit As IGeoDataServerInit
    Dim pGDSchild As IGeoDataServer
    Dim pGDSchildInit As IGeoDataServerInit
    Dim bOK As Boolean
    
    '++ Initialize the parent
    Set pGDSparent = New GeoDataServer
    Set pGDSparentInit = pGDSparent
    pGDSparentInit.InitFromConnectionString "SERVER=bobmk;INSTANCE=5165;VERSION=sde.DEFAULT;USER=qa;PASSWORD=qa;DATABASE=qa1"
    
    '++ Initialize the child
    '++ (note: this must reference an already exisiting and empty personal geodatabase)
    Set pGDSchild = New GeoDataServer
    Set pGDSchildInit = pGDSchild
    pGDSchildInit.InitFromFile "d:\temp\CO_child.mdb"
    
    '++ Create the replica
    bOK = Create_Replica(pGDSparent, pGDSchild, "sample_COReplica", esriReplicaAccessNone)
    MsgBox "Created replica successfully: " & bOK
    
End Sub

'++ Creates a 2 way replica using ArcSDE geodatabases. The geodatabases are
'++ accessed over the LAN.

Sub Create_2wayReplica_LAN()
    
    Dim pGDSparent As IGeoDataServer
    Dim pGDSparentInit As IGeoDataServerInit
    Dim pGDSchild As IGeoDataServer
    Dim pGDSchildInit As IGeoDataServerInit
    Dim bOK As Boolean
    
    '++ Initialize the parent
    Set pGDSparent = New GeoDataServer
    Set pGDSparentInit = pGDSparent
    pGDSparentInit.InitFromConnectionString "SERVER=bobmk;INSTANCE=5165;VERSION=sde.DEFAULT;USER=qa;PASSWORD=qa;DATABASE=qa1"
    
    '++ Initialize the child
    Set pGDSchild = New GeoDataServer
    Set pGDSchildInit = pGDSchild
    pGDSchildInit.InitFromConnectionString "SERVER=bobmk;INSTANCE=5166;VERSION=sde.DEFAULT;USER=qa;PASSWORD=qa;DATABASE=qa2"
    
    '++ Create the replica
    bOK = Create_Replica(pGDSparent, pGDSchild, "sample_2WayRep_LAN", esriReplicaBothReadWrite)
    MsgBox "Created replica successfully: " & bOK
    
End Sub

'++ Creates a 2 way replica using ArcSDE geodatabases. Remote GeoDataServer
'++ objects served by ArcGIS server over the WAN are used.

Sub Create_2wayReplica_WAN()
    
    Dim pGDSparent As IGeoDataServer
    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
    
    '++ 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 = "bobmk_5165_qa"
        Set pSon = pSons.Next
    Loop
    Set pName = pSon
    Set pGDSparent = pName.Open
    
    '++ 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_5161"
        Set pSon = pSons.Next
    Loop
    Set pName = pSon
    Set pGDSchild = pName.Open
    
    '++ Create the replica
    bOK = Create_Replica(pGDSparent, pGDSchild, "sample_2WayRep_WAN", esriReplicaBothReadWrite)
    MsgBox "Created replica successfully: " & bOK
    
End Sub

'++ Creates a replica of all data in a feature dataset named "SampleDataset"

Function Create_Replica(pGDSparent As IGeoDataServer, pGDSchild As IGeoDataServer, sRepName As String, lAccType As esriReplicaAccessType) As Boolean
    On Error GoTo eh
    
    Dim pRepAgent As IReplicationAgent
    Dim pReplicaOptions As IGPReplicaOptions
    Dim pGPReplicaDesc As IGPReplicaDescription
    Dim pGPReplicaDatasets As IGPReplicaDatasets
    Dim pGPReplicaDatasets_Expand As IGPReplicaDatasets
    Dim pGPReplicaDataset As IGPReplicaDataset
    
    '++ 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 = (lAccType = esriReplicaAccessNone)
    End With
    
    '++ Set the replica options
    Set pReplicaOptions = New GPReplicaOptions
    With pReplicaOptions
        .AccessType = lAccType
        .ChildReconcilePolicy = esriReplicaResolveConflictsNone
        .ParentReconcilePolicy = esriReplicaResolveConflictsNone
        .IsChildFirstSender = True
    End With
    
    '++ Create the replica
    Set pRepAgent = New ReplicationAgent
    pRepAgent.CreateReplica "", pGDSparent, pGDSchild, sRepName, pGPReplicaDesc, pReplicaOptions
    
    Create_Replica = True
    Exit Function
eh:
    MsgBox "Create replica errored: " & Err.Description & " : error number: " & Err.Number
End Function