Synchronizing content using the API

When using Content staging, you can use the Kentico API to perform your own synchronization process. There are several methods you can use to work with synchronization.

Namespace

Method

Description

CMS.DocumentEngine

DocumentSynchronizationHelper.LogDocumentChange

Creates synchronization tasks for the specified page, or a set of pages under the specified path. The method returns the resulting tasks in a list.

CMS.Synchronization

SynchronizationHelper.LogObjectChange

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

CMS.Synchronization

StagingTaskRunner.RunSynchronization

Runs the synchronization of specified tasks. To call the method, you must first create a StagingTaskRunner instance.

Important: The StagingTaskRunner class is defined in the CMS.SynchronizationEngine assembly. When using the API in a custom project or assembly, you need to add the appropriate reference.

Examples

Pages

The following example shows how to synchronize the content of the page /Home to server Staging.Target1:




using System.Collections.Generic;
using System.Linq;

using CMS.DataEngine;
using CMS.DocumentEngine;
using CMS.Membership;
using CMS.SiteProvider;
using CMS.Synchronization;
// The project/assembly containing the code must also have a reference to the CMS.SynchronizationEngine assembly

...

TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

// Gets the page (tree node) for synchronization
TreeNode node = tree.SelectSingleNode(SiteContext.CurrentSiteName, "/Home", "en-us", false, null, false);

if (node != null)
{
    // Gets the target server
    ServerInfo si = ServerInfoProvider.GetServerInfo("Staging.Target1", SiteContext.CurrentSiteID);

    if (si != null)
    {
        // Logs the synchronization tasks
        List<ISynchronizationTask> tasks = DocumentSynchronizationHelper.LogDocumentChange(node, TaskTypeEnum.UpdateDocument, true, false, tree, si.ServerID, null, false);

        // Gets the IDs of the tasks and runs the synchronization
        new StagingTaskRunner(si.ServerID).RunSynchronization(tasks.Select(t => t.TaskID));
    }
}


Objects

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




using System.Collections.Generic;
using System.Linq;

using CMS.DataEngine;
using CMS.Membership;
using CMS.SiteProvider;
using CMS.Synchronization;
// The project/assembly containing the code must also have a reference to the CMS.SynchronizationEngine assembly

...

// Gets the object for synchronization
UserInfo userObj = UserInfoProvider.GetUserInfo("administrator");

if (userObj != null)
{
    // Gets the target server
    ServerInfo si = ServerInfoProvider.GetServerInfo("Staging.Target1", SiteContext.CurrentSiteID);

    if (si != null)
    {
        // Logs the synchronization tasks
        List<ISynchronizationTask> tasks = SynchronizationHelper.LogObjectChange(userObj.TypeInfo.ObjectType, 0, userObj.UserLastModified, TaskTypeEnum.UpdateObject, true, false, false, false, false, SiteContext.CurrentSiteID, si.ServerID);

        // Gets the IDs of the tasks and runs the synchronization
        new StagingTaskRunner(si.ServerID).RunSynchronization(tasks.Select(t => t.TaskID));
    }
}