Copied to clipboard

Flag this post as spam?

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


  • Profiterole 232 posts 264 karma points
    Oct 23, 2010 @ 19:44
    Profiterole
    0

    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.

  • Richard Soeteman 4049 posts 12922 karma points MVP 2x
    Oct 24, 2010 @ 09:19
    Richard Soeteman
    0

    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:

    • Umbraco
    • CMS
    • BusinessLogic
    • Interfaces

    CMSImport has 4 events,

    • RecordImporting, fires before a record is imported
    • RecordImported, fires when a record is imported
    • Importing, fires when the import process starts
    • Imported, fires when the import is finished

    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

     

    using

     

    System;

    using

     

    System.Collections.Generic;

    using

     

    System.Text;

    using

     

    umbraco.BusinessLogic;

    using

     

    CMSImport.Extensions.Import;

    using

     

    umbraco.cms.businesslogic.web;

    using

     

    umbraco.cms.businesslogic.template;

    namespace

     

    AssignTemplate

    {

     

    public class AssignTemplateUsingDatasource :ApplicationBase

    {

     

    public AssignTemplateUsingDatasource()

    {

     

    ContentImport.RecordImporting += new ContentImport.RecordImportingEventHandler(ContentImport_RecordImporting);

    }

     

    void ContentImport_RecordImporting(object sender, RecordImportingEventArgs e)

    {

     

    Document doc = sender as Document;

     

    if (doc != null)

    {

     

    //Replace TemplateAlias with your column name of the datasource

     

    string templateAlias = e.Items["TemplateAlias"].ToString();

    doc.Template =

    Template.GetByAlias(templateAlias).Id;

    }

    }

    }

    }

  • Richard Soeteman 4049 posts 12922 karma points MVP 2x
    Oct 24, 2010 @ 09:20
    Richard Soeteman
    0

    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

  • Profiterole 232 posts 264 karma points
    Oct 24, 2010 @ 12:23
    Profiterole
    0

    Thank you, I'll try to do it myself... I need to learn it :)

  • singer777 69 posts 80 karma points
    Dec 05, 2011 @ 05:16
    singer777
    0

    Import as a unique doctype and make the template you want the default for that doctype. This works great!

  • Amir Khan 1287 posts 2744 karma points
    Apr 02, 2014 @ 00:26
    Amir Khan
    0

    Is there any breaking changes in newer versions of CMS Import that would prevent this from working properly? Just seems to do nothing...

  • Amir Khan 1287 posts 2744 karma points
    Apr 02, 2014 @ 00:41
    Amir Khan
    0

    So I'm guessing it has to do with this, how can I also trigger this on updated?

    e.ImportAction == ImportActions.ImportAsNew
  • Richard Soeteman 4049 posts 12922 karma points MVP 2x
    Apr 02, 2014 @ 07:08
    Richard Soeteman
    0

    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

  • Amir Khan 1287 posts 2744 karma points
    Apr 02, 2014 @ 16:36
    Amir Khan
    0

    I tried using ImportActions.Overwrite, should I just remove the if statment all together you think?

  • Richard Soeteman 4049 posts 12922 karma points MVP 2x
    Apr 02, 2014 @ 16:40
    Richard Soeteman
    0

    Hi Amir,

    I'm not sure I understand. Can you share your code snippet?

    Thanks,

    Richard

  • Amir Khan 1287 posts 2744 karma points
    Apr 02, 2014 @ 16:45
    Amir Khan
    0

    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

    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;
    }
    }
    }
    }
    }
    }

  • Richard Soeteman 4049 posts 12922 karma points MVP 2x
    Apr 02, 2014 @ 16:52
    Richard Soeteman
    0

    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

  • Amir Khan 1287 posts 2744 karma points
    Apr 02, 2014 @ 16:57
    Amir Khan
    0

    So as simple as this?

     

     //Template is found assign it to the document
                            doc.Template = template.Id;
                            doc.Save();

  • Richard Soeteman 4049 posts 12922 karma points MVP 2x
    Apr 02, 2014 @ 17:03
    Richard Soeteman
    1

    Yes :)

  • Amir Khan 1287 posts 2744 karma points
    Apr 02, 2014 @ 19:52
    Amir Khan
    0

    That'll do it! Thanks Richard!

Please Sign in or register to post replies

Write your reply to:

Draft