Using API to synchronize documents

In special cases, you may need to use Kentico CMS API to perform your own synchronization process. There are several methods you can use to work with synchronization.

 

WorkflowEngine.DocumentHelper.LogSynchronization

Creates a synchronization task for the specified document. This method does not actually perform the synchronization, it only creates the task which can be later synchronized. By default, the method creates the task for all the enabled servers. You can use the overriden version with serverId parameter to specify the particular server.

CMSStaging.StagingHelper.RunSynchronization

Runs the synchronization of specified task for the specified server or servers.

 

Here is an example code how to synchronize the content of the document “/Home” to server “Staging.Target1”:

 

[C#]

 

TreeProvider tree = New TreeProvider(CMSContext.CurrentUser);

// Get the base document record

TreeNode node = tree.SelectSingleNode(CMSContext.CurrentSite.SiteName, "/Home", "en-us", FalseNullFalse);

If (node != Null)

{

  // Get full document record (required for synchronization logging)

  node = tree.SelectSingleNode(CMSContext.CurrentSite.SiteName, node.NodeAliasPath, node.DocumentCulture, False, node.NodeClassName, False);

  If (node != Null)

  {

    // Get the server

    ServerInfo si = ServerInfoProvider.GetServerInfo("Staging.Target1", CMSContext.CurrentSite.SiteName);

    If (si != Null)

    {

      // Log the synchronization task for the given server

      TaskInfo ti = DocumentHelper.LogSynchronization(node, TaskTypeEnum.UpdateDocument, tree, si.ServerID);

      If (ti != Null)

      {

        // Run the task synchronization

        StagingHelper.RunSynchronization(ti.TaskID, si.ServerID);

      }

    }

  }

}

 

You can also use low level operations from TaskInfoProvider, SynchronizationInfoProvider and ServerInfoProvider to achieve synchronization, but previous methods should be enough to perform any simple synchronization actions.