Send mail to the author(s) E-mail

View Richard Soeteman's profile on LinkedIn

RSS 2.0 | Atom 1.0 | CDF




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

Sign In


# Thursday, 05 December 2013
Thursday, 05 December 2013 14:52:40 (GMT Standard Time, UTC+00:00) ( CMSImport | Umbraco )

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

I’ve just released version 2.5 of CMSImport. This version is optimized for Umbraco V7 and should work as expected on older versions.One thing that’s changed is the use of Field adapters, the feature you only use when you need to manipulate data during import.  The Field adapters  still work but there is a better approach now, called Field providers. This change was already on the list of changes for CMSImport version 3 but got into the 2.5 release due a change Data type settings change in Umbraco V7. As you can see in the image below Id’s used to be GUIDS and are changed to alias strings in V7.


What is a Field provider?

A field provider is a class that implements IFieldProvider and gets called during the import process to convert a value to the format Umbraco expects. For the above mentioned data type it will convert text values to the corresponding id values.  Below the Field adapter that is used in CMSImport for the Dropdownlist data type.

   1: /// <summary>
   2: /// Responsible to map the id of the dropdownvalue based on  the text value.
   3: /// Internally Umbraco uses this value.
   4: /// </summary>
   5: [FieldProvider(PropertyEditorAlias = "a74ea9c9-8e18-4d2a-8cf6-73c6206c5da6", Priority = FieldProviderPrio.Medium)]
   6: [FieldProvider(PropertyEditorAlias = "Umbraco.DropDown", Priority = FieldProviderPrio.Medium)]
   7: public class DropdownFieldProvider : IFieldProvider
   8: {
  10:     /// <summary>
  11:     /// Parse the data for the dropdownlist
  12:     /// </summary>
  13:     /// <param name="value">The value to parse</param>
  14:     /// <param name="property">The property</param>
  15:     /// <param name="fieldProviderOptions">The options</param>
  16:     /// <returns></returns>
  17:     /// <exception cref="System.NotImplementedException"></exception>
  18:     public object Parse(object value, Property property, FieldProviderOptions fieldProviderOptions)
  19:     {
  20:         if (!value.IsNullOrEmpty())
  21:         {
  22:             int datatypeId = property.PropertyType.DataTypeDefinition.Id;
  24:             string dropdownValue = PreValueHelper.GetDropdownValueByText(value, datatypeId);
  25:             if (!string.IsNullOrEmpty(dropdownValue))
  26:             {
  27:                 value = dropdownValue;
  28:             }
  29:         }
  30:         return value;  
  31:     }
  32: }

One Field provider for more than one data type

As you can see in the above example. It’s possible to use one Field provider for more than one data type because you need to decorate a Field provider with the FieldProvider attribute. In the PropertyEditorAlias parameter you specify the Alias(Umbraco V7) or GUID(Umbraco V4 and 6). You can add as many attributes as you like, its also possible to have more than one Field provider for a single data type.

The second parameter is the priority which describes the order of execution. In case you have some data that is not 100% correct you can change this before it get’s parsed by the default Field providers of CMSImport. The default priority of all Field providers used by CMSImport are Medium. Use High priority if you want to parse the data before the default Field provider parses the data. Or use Low priority when you want to parse data after the the default Field provider parsed the data.

So when you map the following data against the dropdown list:

  • Cars
  • Car’s
  • Carss

You probably want to make sure this gets all import as Cars. Really easy to do with a Field provider. As you can see in the example below I’ve implemented a Field provider gave it a high priority so it will be picked up before the default one and return Cars when the value starts with car.

   1: using System;
   2: using CMSImport.Extensions.FieldProvider;
   3: using CMSImportLibrary.TypeExtensions;
   4: using;
   6: namespace CMSImportLibrary.Providers.FieldProviders.DefaultFieldProviders
   7: {
   8:     [FieldProvider(PropertyEditorAlias = "a74ea9c9-8e18-4d2a-8cf6-73c6206c5da6", Priority = FieldProviderPrio.High)]
   9:     [FieldProvider(PropertyEditorAlias = "Umbraco.DropDown", Priority = FieldProviderPrio.High)]
  10:     public class DropdownFixFieldProvider : IFieldProvider
  11:     {
  12:         public object Parse(object value, Property property, FieldProviderOptions fieldProviderOptions)
  13:         {
  14:             return !value.IsNullOrEmpty() && value.ToString().StartsWith("car", StringComparison.InvariantCultureIgnoreCase) ? "Cars" : value;
  15:         }
  16:     }
  17: }
Comments are closed.