---
title: Synchronizing content using the API
related:
  - https://docs.kentico.com/13/custom-development/handling-global-events/excluding-content-from-staging-and-integration.md
  - https://docs.kentico.com/13/custom-development/handling-global-events/automatically-synchronizing-staging-and-integration-tasks.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

When using [Content staging](https://docs.kentico.com/13/deploying-websites/content-staging.md), 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.                            |

> **Note:** **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**:

```csharp

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.

```csharp

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

```
