in this How To we look at how to perform basic field mapping for simple types.

Our solution has a template with the following fields:

  • Title - Single Line Text
  • Text - Rich Text
  • Number - Number
  • Date - Date Time
  • Checked - Checkbox

Using Auto-Mapping we can define the following model:

public class MyModel{

    public virtual string Title { get; set; }
    public virtual string Text{ get; set; }
    public virtual int Number{ get; set; }
    public virtual DateTime DateTime { get; set; }

}

When using Auto-Mapping the property name must match the field names.

Glass.Mapper.Sc uses the return type of the property to decide how to convert the Sitecore data. It DOES NOT use the Sitecore field type.

Some fields will contain space or characters that cannot be converted to valid C# property names and therefore Auto-Mapping will not work. For these fields we must use configuration.

This configuration can be defined using attribute configuration or fluent configuration:

// Attribute Configuration

public class MyModel{

    [SitecoreField("__updated")]
    public virtual DateTime Updated { get; set; } 

}

//Fluent

var loader = new SitecoreFluentConfigurationLoader();
var modelConfig = loader.Add();
modelConfig.Field(x=>x.Updated).FieldName("__updated");