question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Existing Views Are Overwritten When Creating a New Template

See original GitHub issue

Linked to #3494

This issue was closed at the time and haven’t been able to re-open so just raising the issue again here (and have updated for Umbraco 8).

Bug summary

When creating a new template via code, if a view exists with the same name, it is overwritten. I believe there should be a check in place that if the view exists, it’s left alone but the template is added to the database regardless. If the file doesn’t exist, only then should it create a new, blank one.

Specifics

Umbraco version: 8.1.2

Steps to reproduce

Create a new view in ~\Views called Test.cshtml (not through the CMS). Enter some content, for example:

@{
    Layout = null;
}

<h2>Homepage</h2>

Create a component that creates a new template:

using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Models;
using Umbraco.Core.Services;

namespace UmbracoTemplateTest.Components
{
    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class ContentTypeComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.Components().Append<ContentTypeComponent>();
        }
    }

    public class ContentTypeComponent : IComponent
    {
        private readonly IFileService _fileService;

        public ContentTypeComponent(IFileService fileService)
        {
            _fileService = fileService;
        }

        public void Initialize()
        {
            ITemplate template = _fileService.GetTemplate("test");

            if (template == null)
            {
                template = new Template("Test", "test");

                _fileService.SaveTemplate(template);
            }
        }

        public void Terminate() { }
    }
}

See that Test.cshtml has been overwritten by a basic Umbraco template view:

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage

@{
    Layout = null;
}

Expected result

When the template is saved, existing views with the same name aren’t overwritten.

Actual result

A blank/basic view is written into the contents of the view in the place of already existing content.

This was fixed when creating the template through the CMS but, if you create the file then create a template using code, the file is overwritten.

Thanks,

Ben

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
nul800sebastiaancommented, Aug 29, 2019

I am not sure if the FileService should be responsible for that, but I can ask around.

For convenience it would be nice, but for now you could load the file contents yourself;

            var templateAlias = "test";
            ITemplate template = _fileService.GetTemplate(templateAlias);

            if (template == null)
            {
                template = new Template("Test", templateAlias);
                var existingFilePath = HostingEnvironment.MapPath($"~/Views/{templateAlias}.cshtml");
                if (System.IO.File.Exists(existingFilePath))
                {
                    var existingFile = System.IO.File.ReadAllText(existingFilePath);
                    template.Content = existingFile;
                }
                
                _fileService.SaveTemplate(template);
            }
1reaction
Shazwazzacommented, Sep 17, 2019

This has been fixed at various levels a few times but I guess for just specific circumstance, for example, in the implementation of IFileService, in the methods: CreateTemplateForContentType, CreateTemplateWithIdentity any existing view will not be overwritten with checks to GetViewContent.

You can utilize the CreateTemplateWithIdentity instead of doing

 template = new Template("Test", "test");
_fileService.SaveTemplate(template);

and that should solve your problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create From Template overwrites existing Page.
I have the problem. If I create a page from a template it overwrites an existing page with the same name. This does...
Read more >
Database with created time/by properties get overwritten ...
I'd like to find a way to set up my templates so that they do no overwrite either created time or created by...
Read more >
Normal.dotm template overwritten during an update?
On all three of my computers, my highly customized normal.dotm was overwritten on the same day with an essentially blank new template.
Read more >
How do I theme view fields, with custom templates or ...
Creating template file will ensure that someone who is not familiar with Views will have no effect even if they accidentally altered the...
Read more >
I want to set a field by applying a Template, but
I want to set a field by applying a Template, but not overwrite any existing data in the field. Is there a way...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found