Copied to clipboard

Flag this post as spam?

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


  • Gerard Konings 6 posts 55 karma points
    Jun 02, 2014 @ 12:53
    Gerard Konings
    0

    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.

    (I'm using Umbraco 7)

  • Dominik 1 post 50 karma points
    Aug 01, 2014 @ 09:21
    Dominik
    0

    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

  • Dave N 13 posts 34 karma points
    Oct 14, 2014 @ 07:34
    Dave N
    0

    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);
    }

     

     

Please Sign in or register to post replies

Write your reply to:

Draft