This document is archived and information here might be outdated.  Recommended version.


How to synchronize a replica in a connected environment (ArcObjects .NET 10.8 SDK)
ArcObjects Help for .NET developers > ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > Managing data > Working with feature data > Distributed geodatabases > Replication > How to synchronize a replica in a connected environment

How to synchronize a replica in a connected environment


Summary
This topic shows 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 topic assumes that a replica has already been created and is ready to be synchronized.

Synchronizing a replica in a connected environment

Do the following steps 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 for Server. For more information on how to initialize a GeoDataServer, see 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:
Parameter
Description
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 example:
[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 GeoDataServer.
        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 GeoDataServer.
        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 GeoDataServer.
    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 GeoDataServer.
    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






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 Desktop Standard ArcGIS Desktop Standard
ArcGIS Desktop Advanced ArcGIS Desktop Advanced
Engine Developer Kit Engine: Geodatabase Update