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.
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.
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.
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.
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
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.
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
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
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.
I tried re-loading the nodes and nothing new.
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.
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
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?
Regardless of how you are actually setting the razor code inside your template, a new template can be created programmatically this way:
Awesome stuff. Thanks :)
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:
is working on a reply...