Kentico CMS 6.0 Developer's Guide

Synchronization using API

Synchronization using API

Previous topic Next topic Mail us feedback on this topic!  

Synchronization using API

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

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

 

DocumentEngine.DocumentSynchronizationHelper.LogDocumentChange

Creates synchronization tasks for the specified document or a set of documents under the specified path. The method returns the resulting tasks in a list. Loop through the list to synchronize the tasks.

CMS.Synchronization.SynchronizationHelper.LogObjectChange

Creates synchronization tasks for the specified object under the current site. The method returns the resulting tasks in a list. Loop through the list to synchronize the tasks.

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#]

 

using CMS.CMSHelper;

using CMS.DocumentEngine;

using CMS.SettingsProvider;

using CMS.Synchronization;

 

TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

// Get the base document record

TreeNode node = tree.SelectSingleNode(CMSContext.CurrentSiteName, "/Home", "en-us", false, null, false);

if (node != null)

{

  // Get the server

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

  if (si != null)

   {

     

      // Log the synchronization task

      List<ISynchronizationTask> tasks = DocumentSynchronizationHelper.LogDocumentChange(node, TaskTypeEnum.UpdateDocument, tree, false, tree, si.ServerID, null, false);

 

      // Loop through the list of tasks

      foreach (TaskInfo ti in tasks)

       {

          // 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. Though the previous operations are enough to perform any simple synchronization action.

 

The following example shows how you can synchronize the administrator user account. Synchronization of any other objects is done the same way using the API.

 

[C#]

 

using CMS.SiteProvider;

using CMS.Synchronization;

using CMS.CMSHelper;

 

// Gets the object

UserInfo userObj = UserInfoProvider.GetUserInfo("administrator");

if (userObj != null)

{

    // Gets the server

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

    if (si != null)

    {

        // Log the synchronization task

      List<ISynchronizationTask> tasks = SynchronizationHelper.LogObjectChange(userObj.ObjectType, 0, userObj.UserlastModified, TaskTypeEnum.Object, true, false, false, false ,false, CMSContext.CurrentSiteID);

 

        // Loop through the list of tasks

      foreach (TaskInfo ti in tasks)

        {

            // Run the task synchronization

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

        }

    }

}