How to assign a new page template to an existing document in the tree ? (K8/K9)

Sergiu Filip asked on March 16, 2018 15:14

Apparently, just assigning the ID of the new template to the TreeNode object does not seem to do it, in spite of not raising any errors (i.e. to assignment succeeds, you can Publish() etc.)

Any help will be greatly appreciated !

Correct Answer

Peter Mogilnitski answered on March 18, 2018 13:06

Don't forget you can assign template on the document level and on the node level (take a look at db structure). i.e. you can have a unique template per each language or just one template for all languages, in terms of sql 2 table are involved here :

-- cms_tree represent node information
select NodeTemplateID, NodeTemplateForAllCultures from  cms_tree where NodeID = 1 // Node level
-- cms_document represents document information per language
select DocumentPageTemplateID from  cms_Document where documentNodeID = 1 // Document level

So what template is gonna be used depends on NodeTemplateForAllCultures. If it is true then NodeTemplateID is used, false DocumentPageTemplateID is used.

So in C# terms you must set :

page.NodeTemplateForAllCultures
page.NodeTemplateID
page.DocumentPageTemplateID

I would assume you have one template for all languages then the code should be:

     node.NodeTemplateID = ptiToUpdate.PageTemplateId;
     node.NodeTemplateForAllCultures = true // just to be sure

In terms of interface NodeTemplateForAllCultures(true/false) are 2 last radio buttons: Image Text

0 votesVote for this answer Unmark Correct answer

Recent Answers


Peter Mogilnitski answered on March 16, 2018 15:54 (last edited on March 16, 2018 15:59)

Go to page properties tab - select template and click select and pick a new template. Here is the documentation.

0 votesVote for this answer Mark as a Correct answer

Sergiu Filip answered on March 16, 2018 17:34

OH, sorry - my apologies. I meant PROGRAMMATICALLY, not using the admin interface, LOL.

Sorry for the confusion. Thank you Peter, I meant using the API.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on March 16, 2018 19:53 (last edited on March 16, 2018 19:57)

Pages API and Template API

// Gets a page that want to update
TreeNode page = tree.SelectNodes()
    .Path("/Articles/Coffee-Beverages-Explained")
    .OnCurrentSite()
    .Culture("en-us")
    .FirstObject;
var pageTemplateInfo = PageTemplateInfoProvider.GetPageTemplateInfo("TempletaCodeName")
if (pageTemplateInfo != null) {
    page.DocumentPageTemplateID = pageTemplateInfo.PageTemplateId
}
page.Update()
0 votesVote for this answer Mark as a Correct answer

Sergiu Filip answered on March 17, 2018 00:35

-- That is exactly how I try to do (below is a fragment); no errors, yet the outcome is not the desired one (I refresh the tree in CMS Desk, the document shows the old template being assigned)

            PageTemplateInfo ptiToUpdate = PageTemplateInfoProvider.GetPageTemplateInfo(tplToUpdate);
            node.DocumentPageTemplateID = ptiToUpdate.PageTemplateId;
            node.Update();
            if(null != node.GetWorkflow())
                node.Publish();

            AddToLog(node.DocumentUrlPath, node.ClassName, pti, ptiToUpdate);
0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on March 18, 2018 15:51

If you're using versioning and checkin/out, you'll want to be sure you check the page out before assigning the template and then check it back in as well. If not then version history takes over and that will be used.

0 votesVote for this answer Mark as a Correct answer

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