Copied to clipboard

Flag this post as spam?

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


  • Ben Divsalar 14 posts 34 karma points
    Mar 10, 2014 @ 12:18
    Ben Divsalar
    0

    How to Delete All Templates in C#

    Could someone please let me know the best way to delete all templates in Umbraco API 6.1.6

    This is my code:

           var fs = Services.FileService;
           var allTemplates = fs.GetTemplates();

           foreach (var item in allTemplates)
                {
                    fs.DeleteTemplate(item);
                }

    Because I have master templates, I am getting " The DELETE statement conflicted with the REFERENCE constraint "FK_cmsTemplate_cmsTemplate"  error. I should actually delete the child template first and then delete the master one, and this cannot be controlled by GetTemplates() method.

    Could you let me know if there is any way to check the template is a master template? I cannot find any public property in Template class.

    Please help.

     

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Mar 10, 2014 @ 14:26
    Andy Butland
    0

    I can't see that you can do this - as you say, ITemplate (which is what "item" above will be) doesn't expose the master template Id.  Looking at the source I can see the repository layer does retrieve this (there's a property called Master on TemplateDto) - but it's not passed back through the FileService.  I think you should raise an issue for this at http://issues.umbraco.org/

    For now you could maybe hack it with something like this (untested code)?:

    var deletedAll = false;
    while (!deletedAll) 
    {
      var allTemplates = fs.GetTemplates();
      deletedAll = true;
      foreach (var item in allTemplate)
      {
        try
        {
          fs.DeleteTemplate(item);
        }
        catch (SqlException ex)
        {
          deletedAll = false;
        }
      }
    }
    
  • Ben Divsalar 14 posts 34 karma points
    Mar 10, 2014 @ 14:56
    Ben Divsalar
    0

    Many thanks Andy, I will raise an issue to expose this property in ITemplate. Here is my working code as you suggested:

                var fs = Services.FileService;

                var deletedAll = false;
                while (!deletedAll)
                {
                    var allTemplates = fs.GetTemplates();
                    deletedAll = true;
                    foreach (var item in allTemplates)
                    {
                        try
                        {
                            fs.DeleteTemplate(item.Alias);
                        }
                        catch (SqlException ex)
                        {
                            deletedAll = false;
                        }
                    }
                }

  • Charles Afford 1163 posts 1709 karma points
    Mar 11, 2014 @ 20:26
    Charles Afford
    0

    What version of Umbraco are you using?  What you are doing above seems wrong?  Why do you wanna delete templates in c'#?  Is there a method in the contentService to do this?

    Charlie

  • Ben Divsalar 14 posts 34 karma points
    Mar 11, 2014 @ 23:05
    Ben Divsalar
    0
    Hi Charlie,

    I am migrating an existing big database with lots of document types and content into less number of document types by mapping the existing one into the new one. This process is slightly complicated however I can explain in details if it's required. I have almost done the data migration for document types and contents, now I want to remove all templates as the last step. We want to write the template from scratch. This is as part of an upgrade from v4.11.10 to v6.1.6.
    The API I am using in c# is v6.1.6

    I couldn't find any method in content services however the above methods looks fine in my spike. I haven't ran it in the actual big database yet.

    If you have any better solution to remove the templates please advise.

    Regards

    Ben
  • Ben Divsalar 14 posts 34 karma points
    Mar 11, 2014 @ 23:10
    Ben Divsalar
    0
    Hi Charlie,

    I am migrating an existing big database with lots of document types and content into less number of document types by mapping the existing one into the new one. This process is slightly complicated however I can explain in details if it's required. I have almost done the data migration for document types and contents, now I want to remove all templates as the last step. We want to write the template from scratch. This is as part of an upgrade from v4.11.10 to v6.1.6.
    The API I am using in c# is v6.1.6

    I couldn't find any method in content services however the above methods looks fine in my spike. I haven't ran it in the actual big database yet.

    If you have any better solution to remove the templates please advise.

    Regards

    Ben
  • Charles Afford 1163 posts 1709 karma points
    Mar 13, 2014 @ 10:04
    Charles Afford
    0

    Hi Ben,

    Sorry for late reply.  I am sure what you are doing is fine and i did not know if there was a method for removing doc types.

    It was just a word a caution really as i have seen other instances where Umbraco database has been modified and everything has failed.

    But it sounds like its all working :).

    Let me know the result.

     

    Charlie :)

     

Please Sign in or register to post replies

Write your reply to:

Draft