Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • keilo 568 posts 1023 karma points
    Nov 08, 2015 @ 07:37
    keilo
    0

    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;

    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); 
                     }
        }
    
  • gary 385 posts 916 karma points
    Nov 08, 2015 @ 13:09
    gary
    0

    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

  • keilo 568 posts 1023 karma points
    Nov 16, 2015 @ 14:50
    keilo
    0

    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.

    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); 
                     }
        }
    
  • Joss 10 posts 73 karma points
    Jan 20, 2016 @ 10:22
    Joss
    0

    Hi, did you ever find a solution to this? Facing the same issue.

  • Andrew Knox 4 posts 27 karma points
    Apr 28, 2017 @ 13:26
    Andrew Knox
    2

    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);
        }
    }
    
  • Proxicode 127 posts 323 karma points
    Dec 30, 2018 @ 20:46
    Proxicode
    0

    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 :-)

Please Sign in or register to post replies

Write your reply to:

Draft