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.
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;
}
}
}
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?
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.
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.
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.
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)?:
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;
}
}
}
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
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 :)
is working on a reply...