Navigatie

Contact

Send mail to the author(s) E-mail

View Richard Soeteman's profile on LinkedIn

RSS 2.0 | Atom 1.0 | CDF

Archief

Categorieën

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Sign In

Zoeken

# Friday, 29 April 2011
Friday, 29 April 2011 13:06:57 (GMT Daylight Time, UTC+01:00) ( CMSImport | Umbraco )

This Blog is not active anymore, further posts will be available on my company website. Read new posts here

A question I get asked a lot is if it is possible to assign a template based on a datasource value. The answer is yes, normally the default assigned template will be assigned when importing your data and this is what 99% of the users want and expect. But using the RecordImported Event we can get the datasource value from the database and use that alias to map the template against the imported document.

Example

In this example we have some data coming from a database that we want to a standard Runway text page. Some items such as Integer and Donec are really important and we want to display these items in Pink. In Umbraco we added a new template called PinkTemplate and as you can see in the image below we added that Template alias in our Datasource that we are going to import.

Example data

As I mentioned earlier we can use the event system to manipulate the document during import . You can use the event system for CMSImport in the same way you use the normal Umbraco event system. In the example below you see that we derive from ApplicationBase and wire up the RecordImported event in the constructor. Since we only want to set the template during the initial import, we first check if the Importaction is  ImportAsNew. Then we need to check if the template column from the datasource is not null. You can access all  items from the datasource  using the e.Items collection. When the template column is not null, we get the template id from the database and assign that value to the document.

   1:  using CMSImport.Extensions.Import;
   2:  using umbraco.BusinessLogic;
   3:  using umbraco.cms.businesslogic.template;
   4:  using umbraco.cms.businesslogic.web;
   5:   
   6:  namespace AssignTemplate
   7:  {
   8:      /// <summary>
   9:      /// This class wil automatically assign a template to the document when being imported for the first time using the RecordImporting Event
  10:      /// If you want to use this class in your own project simply rename e.Items["Template"] to the template column you use
  11:      /// in your datasource
  12:      /// 
  13:      /// This code will work in both the Free and PRO edition of CMSImport and is written for version 1.2. 
  14:      /// If you have any questions please use the forum or email support@soetemansoftware.nl 
  15:      /// </summary>
  16:      public class AssignTemplateUsingDatasource : ApplicationBase
  17:      {
  18:          /// <summary>
  19:          /// Initializes a new instance of the <see cref="AssignTemplateUsingDatasource"/> class.
  20:          /// </summary>
  21:          public AssignTemplateUsingDatasource()
  22:          {
  23:              //Whenever a records gets imported 
  24:              ContentImport.RecordImported += new ContentImport.RecordImportedEventHandler(ContentImport_RecordImported);
  25:          }
  26:   
  27:          /// <summary>
  28:          /// When a record got imported this event is raised.
  29:          /// </summary>
  30:          /// <param name="sender">The imported document.</param>
  31:          /// <param name="e">The <see cref="CMSImport.Extensions.Import.RecordImportedEventArgs"/> instance containing the event data.</param>
  32:          private void ContentImport_RecordImported(object sender, RecordImportedEventArgs e)
  33:          {
  34:              //Only assign the value when its imported for the first time
  35:              if (e.ImportAction == ImportActions.ImportAsNew)
  36:              {
  37:                  Document doc = sender as Document;
  38:                  
  39:                  //Get the template value from the datasource. Datasource values are stored in the Items event arguments
  40:                  //Replace templateName with your column name in the datasource!!!
  41:                  string templateAlias = e.Items["templateName"].ToString();
  42:   
  43:                  if (doc != null && !string.IsNullOrEmpty(templateAlias))
  44:                  {
  45:                      //An alias is specified, try to get the template based on the specified alias  
  46:                      Template template = Template.GetByAlias(templateAlias);
  47:                      if (template.Id != null)
  48:                      {
  49:                          //Template is found assign it to the document
  50:                          doc.Template = template.Id;
  51:                      }
  52:                  }
  53:              }
  54:          }
  55:      }
  56:  }

When the import is finished you see that all items are imported and when you we take a look at the Integer document you’ll see that the assigned template is PinkTemplate instead of the default Runway Textpage template.

Pink template assigned

Why isn’t this supported in the UI?

As I mentioned before most people expect to assign the default value. When I include the assign template via datasource functionality in the UI it’s an option that you need to think about before setting or ignoring the option. My idea with CMSImport is that I want it to be a no brainer to use and introducing this option will make it a little bit harder to use. So if you want this I think it’s better to write 10 lines of code instead.

Downloads

Below you can download the complete source code. Change e.Items[“templateName”] to the column name you want to use in your project, build the project and add the DLL to the bin folder of your Umbraco install. That’s all!

Download the source code here

Comments are closed.