As I wrote in the title, CMSImport works great but can I specify a template? I saw that I can specify general properties like nodeName et createDate. But I tried to specify template (or @template) and I doesn't work.
By default it is not possible to assign the template, but CMSImport comes with an API. All you need to do is add a reference to the CMSImport.Extensions DLL and to the umbraco Dll's:
Umbraco
CMS
BusinessLogic
Interfaces
CMSImport has 4 events,
RecordImporting, fires before a record is imported
With the RecordImporting event we have access to the original Data through the Item Collection on the RecordImportingEventArgs. In the example below, I've added TemplateAlias in my datasource. I didn't map the TemplateAlias column, but in the RecordImportingEvent I use the value of this column to determine the Template Id and assign it to doc.Template.
So it's not possible using the wizard, but a few lines of code can make it possible for you.
There should not be a breaking change for this in later versions of CMSImport. Only the underlying Umbraco is changed so that could cause weird behaviour. Events should always fire. Did you have a check for ImportAsNew in your code? if so remove it maybe?
Here you go, I believe I saw this "Overwrite" option in a post by you from a while back. What I'm trying to do is add some field content and switch the template for several already existing nodes.
Thanks a ton for your help, Amir
using CMSImport.Extensions.Import; using umbraco.BusinessLogic; using umbraco.cms.businesslogic.template; using umbraco.cms.businesslogic.web;
namespace AssignTemplate { /// <summary> /// This class wil automatically assign a template to the document when being imported for the first time using the RecordImporting Event /// If you want to use this class in your own project simply rename e.Items["Template"] to the template column you use /// in your datasource /// /// This code will work in both the Free and PRO edition of CMSImport and is written for version 1.2. /// If you have any questions please use the forum or email [email protected] /// </summary> public class AssignTemplateUsingDatasource : ApplicationBase { /// <summary> /// Initializes a new instance of the <see cref="AssignTemplateUsingDatasource"/> class. /// </summary> public AssignTemplateUsingDatasource() { //Whenever a records gets imported ContentImport.RecordImported += new ContentImport.RecordImportedEventHandler(ContentImport_RecordImported); }
/// <summary> /// When a record got imported this event is raised. /// </summary> /// <param name="sender">The imported document.</param> /// <param name="e">The <see cref="CMSImport.Extensions.Import.RecordImportedEventArgs"/> instance containing the event data.</param> private void ContentImport_RecordImported(object sender, RecordImportedEventArgs e) { //Only assign the value when its imported for the first time if (e.ImportAction == ImportActions.Overwrite) { Document doc = sender as Document;
//Get the template value from the datasource. Datasource values are stored in the Items event arguments //Replace templateName with your column name in the datasource!!! string templateAlias = e.Items["templateName"].ToString();
if (doc != null && !string.IsNullOrEmpty(templateAlias)) { //An alias is specified, try to get the template based on the specified alias Template template = Template.GetByAlias(templateAlias); if (template.Id != null) { //Template is found assign it to the document doc.Template = template.Id; } } } } } }
It's the change in Umbraco API that causes this behaviour You need to save the document explicit. After you've set the template Specify doc.Save() then the changes will be saved. And if you remove your if statement that will happen on create and update.
Can I specify template?
Hi,
As I wrote in the title, CMSImport works great but can I specify a template? I saw that I can specify general properties like nodeName et createDate. But I tried to specify template (or @template) and I doesn't work.
Thank you for your help.
Hi,
By default it is not possible to assign the template, but CMSImport comes with an API. All you need to do is add a reference to the CMSImport.Extensions DLL and to the umbraco Dll's:
CMSImport has 4 events,
Registering these events works the same as registering normal umbraco events.
With the RecordImporting event we have access to the original Data through the Item Collection on the RecordImportingEventArgs. In the example below, I've added TemplateAlias in my datasource. I didn't map the TemplateAlias column, but in the RecordImportingEvent I use the value of this column to determine the Template Id and assign it to doc.Template.
So it's not possible using the wizard, but a few lines of code can make it possible for you.
Hope this helps you,
Richard
Markup is a bit messed up, I will use this example in the manual, but if you want to have it mail me richard [at] soetemansoftware.nl
Thank you, I'll try to do it myself... I need to learn it :)
Import as a unique doctype and make the template you want the default for that doctype. This works great!
Is there any breaking changes in newer versions of CMS Import that would prevent this from working properly? Just seems to do nothing...
So I'm guessing it has to do with this, how can I also trigger this on updated?
Hi Amir,
There should not be a breaking change for this in later versions of CMSImport. Only the underlying Umbraco is changed so that could cause weird behaviour. Events should always fire. Did you have a check for ImportAsNew in your code? if so remove it maybe?
Best,
Richard
I tried using ImportActions.Overwrite, should I just remove the if statment all together you think?
Hi Amir,
I'm not sure I understand. Can you share your code snippet?
Thanks,
Richard
Hi Richard,
Here you go, I believe I saw this "Overwrite" option in a post by you from a while back. What I'm trying to do is add some field content and switch the template for several already existing nodes.
Thanks a ton for your help,
Amir
It's the change in Umbraco API that causes this behaviour You need to save the document explicit. After you've set the template Specify doc.Save() then the changes will be saved. And if you remove your if statement that will happen on create and update.
Hope this helps,
Richard
So as simple as this?
//Template is found assign it to the document
doc.Template = template.Id;
doc.Save();
Yes :)
That'll do it! Thanks Richard!
is working on a reply...