Copied to clipboard

Flag this post as spam?

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


  • JJ Kennedy 2 posts 22 karma points
    Jun 04, 2012 @ 04:16
    JJ Kennedy
    0

    Create a Template Programmatically

    Hi There,

    I have been working with umbraco for a few months now and find it very flexible.

    I am just wondering if it is possible to create a template programmatically. I can create document types and content nodes using the Fluent API, but I am stuck creating templates.

    Thanks

     

  • JJ Kennedy 2 posts 22 karma points
    Jun 05, 2012 @ 01:16
    JJ Kennedy
    0

    I figured this out. I simply create a file in the filesystem in the views folder of the umbraco instance and it gets picked up automatically by umbraco.

    Too easy.

  • David Armitage 508 posts 2078 karma points
    Jul 08, 2016 @ 12:51
    David Armitage
    0

    Hi,

    is this still possible in Umbraco 7.4. I have tried this technique but it doesn't seem to be creating the template.

    Has anyone got a solution. It would massively speed up my development time if this was possible somehow.

    Kind Regards

    David

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Jul 08, 2016 @ 13:06
    Nik
    0

    Hi David,

    If you create your template in the Views folder, it should appear under templates in the back office. You may need to reload nodes if you are already in there however.

    Thanks,

    Nik

  • David Armitage 508 posts 2078 karma points
    Jul 11, 2016 @ 04:11
    David Armitage
    1

    Hi Nik,

    Are you sure this is working for Umbraco 7.4. I have been trying this a few times and not managed to get it to work.

    They don't seem to get created in the back-end.

    1. I tried re-loading the nodes and nothing new.

    2. I also tried re-publishing the whole site. That usually fixes up things like this. https://our.umbraco.org/wiki/reference/api-cheatsheet/publishing-and-republishing - again nothing new.

    3. I regularly add partial views manually and I can confirm this instantly get added to the back-end without any trouble but templates not.

    Can any Umbraco team confirm the status on this?

    Kind Regards

    David

  • James Strugnell 84 posts 192 karma points
    Mar 17, 2017 @ 12:06
    James Strugnell
    0

    I'd also be interested to find out how this is achievable? Templates don't automatically show up for me just by adding them to the filesystem...

    Did you solve this David?

  • Carlos Casalicchio 176 posts 735 karma points
    Jan 30, 2018 @ 21:44
    Carlos Casalicchio
    3

    Regardless of how you are actually setting the razor code inside your template, a new template can be created programmatically this way:

    var fs = ApplicationContext.Services.FileService;
                    if (fs.GetTemplate(template.Alias) == null)
                    {
                        Template newTemplate = new Template(template.Name, template.Alias);
                        fs.SaveTemplate(newTemplate);
                    }
    
  • David Armitage 508 posts 2078 karma points
    Jan 31, 2018 @ 02:23
    David Armitage
    0

    Awesome stuff. Thanks :)

  • Jonathon Cove 26 posts 101 karma points
    Aug 23, 2023 @ 09:39
    Jonathon Cove
    2

    For anyone working in v8+ , the FileService is now injected using DI, otherwise the above example is still correct.

    It might be useful to note that the templates are assigned to content items using the .SetDefaultTemplate() method within IContent, and requiring a re-save of the IContent.

    Something like:

    IContentType demoCT = _contentTypeService.Get("demo");
    
    ITemplate demoT = _fileService.GetTemplate("demo");
    if (demoT == null)
    {
        demoT = new Template(_shortStringHelper, "demo".ToFirstUpper(), "demo");
        _fileService.SaveTemplate(demoT);
    }
    
    demoCT.SetDefaultTemplate(demoT);
    _contentTypeService.Save(demoCT);
    
Please Sign in or register to post replies

Write your reply to:

Draft