---
title: Automatically synchronizing staging and integration tasks
related:
  - https://docs.kentico.com/k81/deploying-websites/content-staging.md
  - https://docs.kentico.com/k81/custom-development/miscellaneous-custom-development-tasks/synchronizing-content-using-the-api.md
  - https://docs.kentico.com/k81/integrating-3rd-party-systems/using-the-integration-bus.md
  - https://docs.kentico.com/k81/custom-development/handling-global-events.md
  - https://docs.kentico.com/k81/custom-development/handling-global-events/reference-global-system-events.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).

You can use [event handlers](https://docs.kentico.com/k81/custom-development/handling-global-events.md) to automatically synchronize [content staging](https://docs.kentico.com/k81/deploying-websites/content-staging.md) and [integration bus](https://docs.kentico.com/k81/integrating-3rd-party-systems/using-the-integration-bus.md) tasks. Use the API to synchronize the required tasks during the following events:

- **StagingEvents.LogTask.After**
- **IntegrationEvents.LogInternalTask.After**
- **IntegrationEvents.LogExternalTask.After**

> **Tip:** **Tip**: Access the data of the synchronization task through the **Task** property of the event handler's **StagingLogTaskEventArgs** or **IntegrationTaskEventArgs** parameter.

## Example

The following example demonstrates how to automatically synchronize staging tasks and outgoing integration tasks:

1. [Assign handlers](https://docs.kentico.com/k81/custom-development/handling-global-events.md) to the **StagingEvents.LogTask.After** and **IntegrationEvents.LogInternalTask.After** events. The example uses a custom class in the App\_Code folder:

   ```csharp

   using CMS.Base;
   using CMS.Synchronization;
   using CMS.SiteProvider;

   using CMS.SynchronizationEngine;

   [LogTaskHandlers]
   public partial class CMSModuleLoader
   {
       /// <summary>
       /// Custom attribute class.
       /// </summary>
       private class LogTaskHandlers : CMSLoaderAttribute
       {
           /// <summary>
           /// Called automatically when the application starts
           /// </summary>
           public override void Init()
           {
               // Assigns a handler to the StagingEvents.LogTask.After event
               // This event occurs after the system creates content staging synchronization tasks (separately for each task)
               StagingEvents.LogTask.After += LogStagingTask_After;

               // Assigns a handler to the IntegrationEvents.LogInternalTask.After event
               // This event occurs after the system creates outgoing integration tasks (separately for each task)
               IntegrationEvents.LogInternalTask.After += LogIntegrationTask_After;
           }
       }
   }

   ```
2. Define the handler methods (inside the custom **CMSLoaderAttribute** class):

   ```csharp

   // Automatically synchronizes staging tasks
   private void LogStagingTask_After(object sender, StagingTaskEventArgs e)
   {
       // Gets the target server
       ServerInfo targetServer = ServerInfoProvider.GetServerInfo("MyServer", SiteContext.CurrentSiteID);

       if ((targetServer != null) && (e.Task != null))
       {
           // Synchronizes the processed staging task to the target server
           StagingHelper.RunSynchronization(e.Task.TaskID, targetServer.ServerID);
       }
   }

   // Automatically synchronizes outgoing integration tasks
   private void LogIntegrationTask_After(object sender, IntegrationTaskEventArgs e)
   {
       // Gets the info object for an integration connector
       IntegrationConnectorInfo connectorInfo = IntegrationConnectorInfoProvider.GetIntegrationConnectorInfo("MyConnector");

       if ((connectorInfo != null) && (e.Task != null))
       {
           // Gets an instance of the integration connector class
           BaseIntegrationConnector connector = IntegrationHelper.GetConnector(connectorInfo.ConnectorName) as BaseIntegrationConnector;

           // Synchronizes the processed integration task
           connector.ProcessInternalTask(e.Task);
       }
   }

   ```
3. Save the class.

The handlers ensure that the system immediately synchronizes:

- All staging tasks to a target server
- All outgoing integration tasks using a specified connector
