How to synchronize a replica in a connected environment


This document was published with and applies to ArcGIS 9.3.
A 10 version also exists. A 9.2 version also exists.
Summary This article describes how to synchronize a replica in a connected environment using ArcObjects. A connected environment is one in which both replica geodatabases are connected on a local area network (LAN) or a wide area network (WAN) during replica creation.

This article assumes that a replica has already been created and is ready to be synchronized.

Development licensing Deployment licensing
ArcEditor ArcEditor
ArcInfo ArcInfo
Engine Developer Kit Engine Runtime: Geodatabase Update

To use the code in this article, the following namespaces must be referenced via the using (C#) or Imports (VB.NET) statements. It is also necessary to add the corresponding references to the project in order to gain access to these APIs.

Synchronizing a replica in a connected environment

The following steps describe how to synchronize a replica using ArcObjects. Before running the code, it is assumed that replicas already exist and they are ready to be synchronized.
 
  1. Connect to the geodatabase containing the parent replica and connect to the geodatabase containing the child replica. This requires initializing a GeoDataServer object for each connection. GeoDataServer objects allow access to a geodatabase on a LAN or a WAN through ArcGIS Server. To see how to initialize a GeoDataServer, refer to How to initialize a geodataserver object.
  2. Call the following procedure. To do this, paste it into an application and call it passing in the following parameters:
parentGDS
One of the GeoDataServers initialized in Step 1.
childGDS
The other GeoDataServer initialized in Step 1.
replicaName
The name of the replica to be synchronized.
conflictPolicy
The policy that describes how conflicts will be handled when synchronizing.
syncDirection 
Indicates whether the changes should be sent from the parent to the child or from the child to the parent.
columnLevel
True means that conflicts are defined from the column level, while false means that conflicts are defined from the object level.
 
See the following code:

[C#]
// Synchronizes a replica in a connected environment.
public void SynchronizeReplica(IGeoDataServer parentGDS, IGeoDataServer
  childGDS, String replicaName, esriReplicationAgentReconcilePolicy
  conflictPolicy, esriReplicaSynchronizeDirection syncDirection, Boolean
  columnLevel)
{
  try
  {
    // Iterate through the replicas of the parent geodata server.
    IGPReplicas gpReplicas = parentGDS.Replicas;
    IGPReplica parentReplica = null;
    for (int i = 0; i < gpReplicas.Count; i++)
    {
      // See if the unqualified replica name matches the replicaName parameter.
      IGPReplica currentReplica = gpReplicas.get_Element(i);
      String currentReplicaName = currentReplica.Name;
      int dotIndex = currentReplicaName.LastIndexOf(".") + 1;
      String baseName = currentReplicaName.Substring(dotIndex,
        currentReplicaName.Length - dotIndex);
      if (baseName.ToLower() == replicaName.ToLower())
      {
        parentReplica = currentReplica;
        break;
      }
    }

    // Check to see if the parent replica was found.
    if (parentReplica == null)
    {
      throw new ArgumentException(
        "The requested replica could not be found on the parent GDS.");
    }

    // Iterate through the replica of the child geodata server.
    gpReplicas = childGDS.Replicas;
    IGPReplica childReplica = null;
    for (int i = 0; i < gpReplicas.Count; i++)
    {
      // See if the unqualified replica name matches the replicaName parameter.
      IGPReplica currentReplica = gpReplicas.get_Element(i);
      String currentReplicaName = currentReplica.Name;
      int dotIndex = currentReplicaName.LastIndexOf(".") + 1;
      String baseName = currentReplicaName.Substring(dotIndex,
        currentReplicaName.Length - dotIndex);
      if (baseName.ToLower() == replicaName.ToLower())
      {
        childReplica = currentReplica;
        break;
      }
    }

    // Check to see if the child replica was found.
    if (childReplica == null)
    {
      throw new ArgumentException(
        "The requested replica could not be found on the child GDS.");
    }

    // Synchronize the replica.
    IReplicationAgent replicationAgent = new ReplicationAgentClass();
    replicationAgent.SynchronizeReplica(parentGDS, childGDS, parentReplica,
      childReplica, conflictPolicy, syncDirection, columnLevel);
  }
  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]
' Synchronizes a replica in a connected environment.

Public Sub SynchronizeReplica(ByVal parentGDS As IGeoDataServer, ByVal childGDS As IGeoDataServer, ByVal replicaName As String, ByVal conflictPolicy As esriReplicationAgentReconcilePolicy, ByVal syncDirection As esriReplicaSynchronizeDirection, ByVal columnLevel As Boolean)
    
    Try
    ' Iterate through the replicas of the parent geodata server.
    Dim gpReplicas As IGPReplicas = parentGDS.Replicas
    Dim parentReplica As IGPReplica = Nothing
    For i As Integer = 0 To gpReplicas.Count - 1
        ' See if the unqualified replica name matches the replicaName parameter.
        Dim currentReplica As IGPReplica = gpReplicas.Element(i)
        Dim currentReplicaName As String = currentReplica.Name
        Dim dotIndex As Integer = currentReplicaName.LastIndexOf(".") + 1
        Dim baseName As String = currentReplicaName.Substring(dotIndex, currentReplicaName.Length - dotIndex)
        If baseName.ToLower() = replicaName.ToLower() Then
            parentReplica = currentReplica
            Exit For
        End If
    Next i
    
    ' Check to see if the parent replica was found.
    If parentReplica Is Nothing Then
        Throw New ArgumentException("The requested replica could not be found on the parent GDS.")
    End If
    
    ' Iterate through the replica of the child geodata server.
    gpReplicas = childGDS.Replicas
    Dim childReplica As IGPReplica = Nothing
    For i As Integer = 0 To gpReplicas.Count - 1
        ' See if the unqualified replica name matches the replicaName parameter.
        Dim currentReplica As IGPReplica = gpReplicas.Element(i)
        Dim currentReplicaName As String = currentReplica.Name
        Dim dotIndex As Integer = currentReplicaName.LastIndexOf(".") + 1
        Dim baseName As String = currentReplicaName.Substring(dotIndex, currentReplicaName.Length - dotIndex)
        If baseName.ToLower() = replicaName.ToLower() Then
            childReplica = currentReplica
            Exit For
        End If
    Next i
    
    ' Check to see if the child replica was found.
    If childReplica Is Nothing Then
        Throw New ArgumentException("The requested replica could not be found on the child GDS.")
    End If
    
    ' Synchronize the replica.
    Dim replicationAgent As IReplicationAgent = New ReplicationAgentClass()
    replicationAgent.SynchronizeReplica(parentGDS, childGDS, parentReplica, childReplica, conflictPolicy, syncDirection, columnLevel)
    
    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