I have a number of nodes with DocType Articles where allowed templates are "ArticleView1" and "ArticleView2"
There are also number of articles (100+) where the default template set to none (empty) for these nodes, under Properties the dropdown shows none selected but when queried via Template.Alias they seem to have the old ArticleView1 in Alias. I have already set default template to ArticleView2 in Settings->docType but need to update the old ones and save programmatically.
How can I programmatically set their default (assigned) template value?
I have tried the following but understand the Template.Alias is read-only;
IContentService cs = ApplicationContext.Current.Services.ContentService;
foreach (var page in CurrentPage.DescendantsOrSelf("Articles")){
var content = cs.GetById(page.Id);
if (content.Template.Alias!="ArticleView2" ){
content.Template.Alias="ArticleView2"; // cant do that, its read-only
cs.SaveAndPublish(content);
}
}
Can't answer your question, but can add that on a new install I tried changing the default template, but it does not update in the content section.
It would have to be manually changed to the new template (remains blank), however if you change the settings doctype back to the original default template, it is already selected.
I'm sure that in previous versions you could just change the settings doctype to overwrite the default template and it would update on content pages.
However for this particular instance Im looking for the right method/call to set the "assigned" template alias, to carry it programmatically on number of nodes (one-by-one is not viable).
I tried to read throught documentation and forums but couldnt find the right approach, to update the template for node.
IContentService cs = ApplicationContext.Current.Services.ContentService;
foreach (var page in CurrentPage.DescendantsOrSelf("Articles")){
var content = cs.GetById(page.Id);
if (content.Template.Alias!="ArticleView2" ){
content.Template.Alias="ArticleView2"; // cant do that, its read-only
cs.SaveAndPublish(content);
}
}
I ran into this issue today, thought I'd drop in the code I used as a quick fix to one of my views after migrating 100's of news articles without a template because I forget to set it on the doc type. I loaded the page once, then just removed the code once all the news articles had been updated.
TL;DR You need to pull back the template instance and assign that to the Template property of the content.
var cmsService = DependencyResolver.Current.GetService<IUmbracoCmsService>();
var fileService = DependencyResolver.Current.GetService<IUmbracoFileService>();
var template = fileService.GetTemplateByAlias("NewsArticlePage");
IList<IContent> existingNewsArticles = cmsService.GetContentByDocumentTypeAlias(DocumentTypeConstants.NewsArticlePage.Alias);
foreach (var newsArticle in existingNewsArticles)
{
if (newsArticle.Template == null || newsArticle.Template.Alias != "NewsArticlePage")
{
newsArticle.Template = template;
cmsService.SaveContent(newsArticle);
}
}
How to set the active Template programmatically
I have a number of nodes with DocType Articles where allowed templates are "ArticleView1" and "ArticleView2"
There are also number of articles (100+) where the default template set to none (empty) for these nodes, under Properties the dropdown shows none selected but when queried via Template.Alias they seem to have the old ArticleView1 in Alias. I have already set default template to ArticleView2 in Settings->docType but need to update the old ones and save programmatically.
How can I programmatically set their default (assigned) template value?
I have tried the following but understand the Template.Alias is read-only;
Hi Keilo
Can't answer your question, but can add that on a new install I tried changing the default template, but it does not update in the content section.
It would have to be manually changed to the new template (remains blank), however if you change the settings doctype back to the original default template, it is already selected.
I'm sure that in previous versions you could just change the settings doctype to overwrite the default template and it would update on content pages.
Regards
Gary
Hi Gary
Yes i came across that in previous versions.
However for this particular instance Im looking for the right method/call to set the "assigned" template alias, to carry it programmatically on number of nodes (one-by-one is not viable).
I tried to read throught documentation and forums but couldnt find the right approach, to update the template for node.
Hi, did you ever find a solution to this? Facing the same issue.
I ran into this issue today, thought I'd drop in the code I used as a quick fix to one of my views after migrating 100's of news articles without a template because I forget to set it on the doc type. I loaded the page once, then just removed the code once all the news articles had been updated.
TL;DR You need to pull back the template instance and assign that to the Template property of the content.
Posted an updated solution here https://our.umbraco.com/forum/extending-umbraco-and-using-the-api//95154-assign-template-to-doctype-programmatically-in-razor-code Works with Umbraco 7.12 and runs from inside a template. Thanks Andrew Knox for getting me half way there :-)
is working on a reply...