Vidhyardhi Gorrepati
Vidhyardhi Gorrepati

Reputation: 682

Kentico auto synchronization via API not working

I have written a scheduled task in CMS to create new documents in our test environment and auto-sync the staging tasks to the production environment. The document creation part is working fine and so is the task creation part but I can't figure out why the staging tasks are not synchronized automatically.

Here is the code

    List<ISynchronizationTask> tasks = new List<ISynchronizationTask> ( );
    TreeProvider treeProvider = new TreeProvider ( );
    NodeSelectionParameters nodeSelectionParameters = new NodeSelectionParameters
    {
        AliasPath = "/Path",
    };
    TreeNode parentPage = treeProvider.SelectSingleNode ( nodeSelectionParameters );

    ServerInfo serverInfo = ServerInfoProvider.GetServerInfo ( "server", SiteContext.CurrentSiteID );

    TreeNode newPage = TreeNode.New ( "Class", treeProvider );
    newPage.DocumentName = "Title";
    newPage.DocumentCulture = "en-us";
    newPage.Insert ( parentPage );
    newPage.Publish ( "Created by Scheduled Task" );

    tasks.AddRange ( DocumentSynchronizationHelper.LogDocumentChange ( parentPage, TaskTypeEnum.CreateDocument, true, false, treeProvider, serverInfo.ServerID, null, false ) );

    if ( tasks.Any ( ) )
    {
        foreach ( var sTask in tasks )
        {
            string result = new StagingTaskRunner ( serverInfo.ServerID ).RunSynchronization ( sTask.TaskID );
        }
    }

The result string from RunSynchronization method always returns a null or empty string.

Also not sure if the tasks are getting created by newPage.Publish or DocumentSynchronizationHelper.LogDocumentChange method

Upvotes: 0

Views: 155

Answers (1)

Trevor F
Trevor F

Reputation: 1437

Log change logs the change in the staging, but you may want to just grab the task using the normal API first, instead of using the tasks variable.

A couple things could be happening. First I would break point right after you create the task and check the staging task table in the database, it's possible that the actually creation is done after or on a separate thread so you don't actually have access to it when the code runs or it's trying to submit it before it's created.

I honestly would just create a separate task to auto push, or add the logic in the task created global event, then push it.

Otherwise another thing is to make sure staging is enabled, even if you want to push something if staging isn't set up it won't.

Upvotes: 2

Related Questions