Copying CMS.File cause file loses on target page

Tomasz Czado asked on May 29, 2018 09:48

Hi, I have a problem when copying CMS.File from one location to another via Kentico API. The page under new location loses the file.

Here is the code:

var tree = new TreeProvider(MembershipContext.AuthenticatedUser);
var newNode = tree.CopyNode(sourcePage, targetPage, true);

newNode .DocumentName = string.Format("{0}_{1}", newNode .DocumentName, "archive");
newNode .NodeAlias = newNode .DocumentName;

PublishDocument(SiteContext.CurrentSite, newNode , tree);

And PublishDocument method has a definition as you can see below:

var siteName = siteInfo.SiteName;

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

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