MVC Templates - Is there a way to see if any templates exist for a page type?

Kentico Dev asked on April 22, 2020 20:37

I have a Landing Page type in my MVC project that I am now converting to use MVC Templates according to these instructions

That all works well now, except for the existing landing pages that were created prior to this change. Since they were created before templates, I get the error Page template not found for selected page. which makes sense since the controller now returns this:

return new TemplateResult(documentID);

Is there a way to check if the current page has any associated templates, and if not, just returns the old view like this?

return View(model);

That way, I can keep my old pages, while moving forward with new landing pages that use templates.

Correct Answer

Dmitry Bastron answered on April 22, 2020 23:20

Hi,

I wouldn't say it is recommended way, but it should be technically possible. I'd recommend performing this transition into templates still in one go if possible meaning that all pages need to be recreated. But if it is the case, this hack should tell you whether the page has template or not:

var templateConfig = document.GetValue("DocumentPageTemplateConfiguration");
if (string.IsNullOrEmpty(templateConfig))
{
    // No template
    HttpContext.Kentico().PageBuilder().Initialize(document.DocumentID);
    return View(model);
}
else
{
    // Template
    return new TemplateResult(document.DocumentID);
}
2 votesVote for this answer Unmark Correct answer

Recent Answers


Kentico Dev answered on April 22, 2020 23:42

Thanks, that works great!

But why do you say it is not the recommended way? What are the downsides of this?

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on April 23, 2020 00:02

Glad it works!

Your existing non-templated pages will work ok, but what is your content transition plan into templated world? Following this advice you shouldn't be able to select template for this existing page, you will be required to create a new page, select your template, copy content from old page (without template) to a new page (with template), publish new page, unpublished or delete old page, fix URLs. If that's ok and this is what you were going to do anyway then it should be fine, I don't see any other issues.

1 votesVote for this answer Mark as a Correct answer

Kentico Dev answered on April 23, 2020 00:10

Yes that it is fine. In some cases I will not need to convert the pages anyway, I will leave them as is.

Thanks again!

0 votesVote for this answer Mark as a Correct answer

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