Scheduled task for synchronization of staging tasks

   —   

In this article you will learn how to create a scheduled task which will synchronize any/certain staging tasks from the staging queue.

This article and its code sample is just a proof of concept and may not cover all possible use cases. You may need to adjust the code to fit your needs. It should give you the idea what needs to be done to synchronize staging tasks using a scheduled job. The code was tested on Kentico 12.

There is the scheduled task for automatic content staging available in the out-of-the-box installation. This task only synchronizes page changes. You cannot use the default Content synchronization task to synchronize changes of objects or custom table data. But sometimes you may want to automatically synchronize other tasks types for certain objects. For example for media files. Or, you want to simply synchronize all the tasks in the queue without going to the Staging application.

The below code snippets are copy&paste from the sample code file, available in a ZIP archive:

   Get the code

At first you need to prepare class file for the custom scheduled task. After you have this ready (you can also register the task into the Kentico Admin UI and then just add the code), you need to get some more details like site info, target server and the list of tasks:

//Get the SiteInfo var siteInfo = SiteInfoProvider.GetSiteInfo(ti.CurrentSiteName); ... // Get the staging server - replace TargetServer with your server code name ServerInfo targetServer = ServerInfoProvider.GetServerInfo("TargetServer", SiteContext.CurrentSiteID); ... // Get all staging tasks that target the given server var tasks = StagingTaskInfoProvider.SelectTaskList(SiteContext.CurrentSiteID, targetServer.ServerID, null, null);

Once you have all the information and data you need, you can check what staging task type you want to synchronize. For example any task of any object type:

foreach (var task in tasks) { //flag to set what tasks to sync isValid = task.TaskType.IsOneOf( //page actions TaskTypeEnum.CreateDocument , TaskTypeEnum.UpdateDocument , TaskTypeEnum.DeleteDocument , TaskTypeEnum.ArchiveDocument , TaskTypeEnum.MoveDocument , TaskTypeEnum.PublishDocument , TaskTypeEnum.RejectDocument , TaskTypeEnum.DeleteAllCultures , TaskTypeEnum.BreakACLInheritance , TaskTypeEnum.RestoreACLInheritance //media library folder actions , TaskTypeEnum.CopyMediaFolder , TaskTypeEnum.CreateMediaFolder , TaskTypeEnum.DeleteMediaFolder , TaskTypeEnum.DeleteMediaRootFolder , TaskTypeEnum.MoveMediaFolder , TaskTypeEnum.RenameMediaFolder //general object actions , TaskTypeEnum.CreateObject , TaskTypeEnum.UpdateObject , TaskTypeEnum.DeleteObject , TaskTypeEnum.AddToSite , TaskTypeEnum.RemoveFromSite );

Or, for example only media library and media file related tasks:

isValid = task.TaskType.IsOneOf( TaskTypeEnum.CreateObject , TaskTypeEnum.UpdateObject , TaskTypeEnum.DeleteObject , TaskTypeEnum.CopyMediaFolder , TaskTypeEnum.CreateMediaFolder , TaskTypeEnum.DeleteMediaFolder , TaskTypeEnum.DeleteMediaRootFolder , TaskTypeEnum.MoveMediaFolder , TaskTypeEnum.RenameMediaFolder ) && (task.TaskObjectType == "media.file" || task.TaskObjectType == "media.folder");

Once you have the desired set of staging tasks, you can run the synchronization:

// Synchronizes the processed staging task to the target server syncResult = str.RunSynchronization(task.TaskID);

And that's basically it! In the sample code is added some logging to the Last result column next to the scheduled task in the Admin UI (screen shot). But you may want to use EventLogProvider.LogInformation method to log e.g. any exceptions so the admins can get email notification form the Event log and take actions.

Share this article on   LinkedIn

Juraj Ondrus

Hi, I am the Technical support leader at Kentico. I'm here to help you use Kentico and get as much as possible out of it.