Copied to clipboard

Flag this post as spam?

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


  • Murray Roke 503 posts 966 karma points c-trib
    Mar 04, 2015 @ 01:57
    Murray Roke
    0

    How do I rename a template programatically

    I wish to change a template named Home to Homepage programatically for a migration script.

    I tried

    var fs = ApplicationContext.Current.Services.FileService;
    var t = fs.GetTemplate("Home");
    t.Name = "Homepage";
    

    but I get the compile error:

    Property or indexer 'Umbraco.Core.Models.IFile.Name' cannot be assigned to -- it is read only
    

    In this case I don't want to rename any files, my Homepage.cshtml will already exist.

    Cheers.
    Murray

  • mike 90 posts 258 karma points
    Mar 04, 2015 @ 03:10
    mike
    0

    I think to do that you must use contentservice as other methods access cache not the database.

  • Kevin Jump 2327 posts 14813 karma points MVP 8x c-trib
    Mar 05, 2015 @ 10:41
    Kevin Jump
    101

    Hi

    you should beable to do this via the FileService that is the new API to manage stylesheets and templates, but i think the seceret truth might be that you have to do it with the legacy API - because it looks like that's what the Umbraco back office is doing.

    tracing through from the template dialog, it looks like the save function for the template fires SaveTemplate in CodeEditorSave webservice.

    https://github.com/umbraco/Umbraco-CMS/blob/dev-v7/src/Umbraco.Web/umbraco.presentation/umbraco/webservices/codeEditorSave.asmx.cs#L436

    which itself is using the old api - but say's its obsoleated by SaveTemplate in the FileSaveController

    https://github.com/umbraco/Umbraco-CMS/blob/ded1def8e2e7ea1a4fd0f849cc7a3f1f97cd8242/src/Umbraco.Web/WebServices/SaveFileController.cs#L164

    Now that uses the old api too, but this might be a good starting point - because without relying to much on old code, you might be able to call this reset service ( so do a HTTP post to /Umbraco/RestServices/SaveFile/SaveTemplate ).

    if that's to fiddly - i think you have to resort to the old api (pre v6/7) stuff, which is in https://github.com/umbraco/Umbraco-CMS/blob/dev-v7/src/umbraco.cms/businesslogic/template/Template.cs

    unlike the new api - you don't run things through the service controller, so you just get a copy of your template by ID, and change Text (which is the old name for Name) and do template.Save();

    so the short answer might be:

    // using umbraco.cms.businesslogic.template
    
    var t = new template(id);
    t.Text = "NewName" ; 
    t.Save(); 
    

    I haven't tried this, i don't know if you have to rename the physical file or if Umbraco will do it for you.

    and all this code is obsolete at some point & it might be worth throwing something into issues.umbraco.org - saying FileService offers no way to rename templates.

  • Murray Roke 503 posts 966 karma points c-trib
    Mar 09, 2015 @ 22:16
    Murray Roke
    0

    Yep thanks. slightly tweaked solution (fix capitalisation, and set Alias):

    var t = new Template(id);
    t.Text = newName;
    t.Alias = newName;
    t.Save();
    

    added issue: http://issues.umbraco.org/issue/U4-6381

Please Sign in or register to post replies

Write your reply to:

Draft