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. 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.

CMS.Synchronization

StagingHelper.RunSynchronization

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

Examples

Pages

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




using System;
using System.Collections.Generic;

using CMS.DocumentEngine;
using CMS.Synchronization;
using CMS.Membership;
using CMS.SiteProvider;
using CMS.DataEngine;

...

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);

        // Loops through the list of tasks
        foreach (StagingTaskInfo sti in tasks)
        {
            // Runs the task synchronization
            StagingHelper.RunSynchronization(sti.TaskID, si.ServerID);
        }
    }
}


You can also use low level members of the StagingTaskInfoProvider, SynchronizationInfoProvider and ServerInfoProvider classes to achieve synchronization.

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;
using System.Collections.Generic;

using CMS.Synchronization;
using CMS.Membership;
using CMS.SiteProvider;
using CMS.DataEngine;

...

// 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);

        // Loops through the list of tasks
        foreach (StagingTaskInfo sti in tasks)
        {
            // Runs the task synchronization
            StagingHelper.RunSynchronization(sti.TaskID, si.ServerID);
        }
    }
}