Publish document doesn't work - workflow + versioning

Tomasz Czado asked on April 19, 2018 08:57

I'm trying to create/update document and then publish it. All works fine in CMS but when I tried to get that document on live site, I still see old version.

The page type is CMS.File and file returned while opening this page on live site is different what I can see in CMS.

I tried with this code:

var workflowManager = WorkflowManager.GetInstance(tree);
        var settingsWorkflow = workflowManager.GetNodeWorkflow(node);
        if (null != settingsWorkflow)
        {
            // node.Update();
            DocumentHelper.UpdateDocument(node);
            node.Publish(string.Format("Automatically published {0} page.", node.DocumentName));
        }
        else
        {
            DocumentHelper.UpdateDocument(node);
        }

Documents are published but latest changes are not available on live site.

I also tried with this code:

https://devnet.kentico.com/questions/how-to-publish-or-unpublish-document-from-user-control-or-custom-module

And nothing seems to work.

Correct Answer

Tomasz Czado answered on April 19, 2018 09:38

Solution:

// Create a new Version manager instance
var manager = VersionManager.GetInstance(tree);

// Check out the document
if (!node.IsCheckedOut)
{
    manager.CheckOut(node);
}

// Save the changes
DocumentHelper.UpdateDocument(node, tree);

// Check in the document
if (node.IsCheckedOut)
{
    manager.CheckIn(node, null, null);
}

var workflowManager = WorkflowManager.GetInstance(tree);
var workflow = workflowManager.GetNodeWorkflow(node);

// apply latest version
manager.ApplyLatestVersion(node);

// Check if the document uses workflow
if (workflow != null)
{
    // Publish the document
    workflowManager.PublishDocument(node, string.Format("Automatically published {0} page.", node.DocumentName));
}
0 votesVote for this answer Unmark Correct answer

   Please, sign in to be able to submit a new answer.