Create Template programmatically, and add to allowedTemplates
Hi,
I'm trying to programmaticly add an allowedTemplate to a contentType. First I need to check if that template exists, if not, I need to create a new one.
I found a forum-post suggesting that you could just put a template in the Views folder and Umbraco would pick it up, it didn't. The umbraco-documentation on templates seems not be be written yet. (http://our.umbraco.org/Documentation/Reference/Management-v6/Models/Template)
So my question is two-fold:
Create a template
Add template to allowedtemplates of a contenttype
I have no clue on how this works. There doesn't seem to be a service like ContentTypeService for templates.
A while ago, but to create a template this bit of code might help for anyone else looking.
private static string ViewPath(string alias) { // may need to change this - this is run from a console app return "../Views/" + alias.Replace(" ", "") + ".cshtml"; }
// // Add an umbraco template for an existing .cshtml file // private static void AddTemplate(string name) { var fs = new FileService(); string path = ViewPath(name); string alias = name.Replace(" ", ""); Template newTemplate = new Template(path, name, alias);
// get the master template ITemplate masterTemplate = fs.GetTemplate("AliasOfTheMasterTemplate"); // get the content of the macro StreamReader streamReader = new StreamReader(path); newTemplate.Content = streamReader.ReadToEnd(); streamReader.Close(); newTemplate.SetMasterTemplate(masterTemplate); fs.SaveTemplate(newTemplate); }
Create Template programmatically, and add to allowedTemplates
Hi,
I'm trying to programmaticly add an allowedTemplate to a contentType. First I need to check if that template exists, if not, I need to create a new one.
I found a forum-post suggesting that you could just put a template in the Views folder and Umbraco would pick it up, it didn't. The umbraco-documentation on templates seems not be be written yet. (http://our.umbraco.org/Documentation/Reference/Management-v6/Models/Template)
So my question is two-fold:
I have no clue on how this works. There doesn't seem to be a service like ContentTypeService for templates.
(I'm using Umbraco 7)
Hi Gerard,
You have to look into FileService.
I found hint here http://our.umbraco.org/documentation/reference/management-v6/Services/ although documentation is not written also.
After some search I found also this snippet: https://github.com/Qite/Umbraco-Inception/blob/a38241ff48f5712f032c07757eecd07fda2e5a14/Umbraco.Inception/CodeFirst/UmbracoCodeFirstInitializer.cs#L115-L142
Kind Regards,
Dominik
A while ago, but to create a template this bit of code might help for anyone else looking.
is working on a reply...