Synchronizing content using the API
When using Content staging, you can use the Xperience API to mange 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
...
// Gets the page (tree node) for synchronization
TreeNode node = new DocumentQuery<TreeNode>()
.Path("/Home", PathTypeEnum.Single)
.OnCurrentSite()
.Culture("en-us")
.TopN(1)
.FirstOrDefault();
if (node != null)
{
// Gets the target server
ServerInfo si = ServerInfo.Provider.Get("Staging.Target1", SiteContext.CurrentSiteID);
if (si != null)
{
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);
// 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 = UserInfo.Provider.Get("administrator");
if (userObj != null)
{
// Gets the target server
ServerInfo si = ServerInfo.Provider.Get("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));
}
}