MultiField type
MultiField type allows us to store complex objects in one field definition.
In the sliders block, for example, we need to store an array of slides into a block's field. Each slide contains LinkText, ImagePointer and LinkToPage.

However, a MultiField cannot contain other MultiFields.
Below you will find a sample code where we perform the following actions:
- Create a MultiField definition.
- Create a group field template and link the MultiField to that template.
- Create a static group and assign some values to the MultiField we have created in the first step.
public void Multi_Field_Group_Sample()
{
//Create MultiField definition
var multiFieldDefinition =
new FieldDefinition<CustomerArea>(this.UniqueString(), SystemFieldTypeConstants.MultiField)
{
Option = new MultiFieldOption
{
IsArray = true,
Fields = new List<string>
{
SystemFieldDefinitionConstants.Name,
SystemFieldDefinitionConstants.Email,
}
}
};
_fieldDefinitionService.Create(multiFieldDefinition);
//Create group field template and assign the MultiField definition to group
var sampleGroupFieldTemplate = new GroupFieldTemplate(this.UniqueString())
{
FieldGroups = new[]
{
new FieldTemplateFieldGroup()
{
Id = "General",
Collapsed = false,
Fields =
{
multiFieldDefinition.Id
}
}
}
};
_fieldTemplateService.Create(sampleGroupFieldTemplate);
//prepare multi field data
var innerData1 = new MultiFieldItem { AreaType = typeof(CustomerArea) };
innerData1.Fields.AddOrUpdateValue(SystemFieldDefinitionConstants.Name, CultureInfo.CurrentCulture, "name1");
innerData1.Fields.AddOrUpdateValue(SystemFieldDefinitionConstants.Email, "email1@domain.com");
var innerData2 = new MultiFieldItem { AreaType = typeof(CustomerArea) };
innerData2.Fields.AddOrUpdateValue(SystemFieldDefinitionConstants.Name, CultureInfo.CurrentCulture, "name2");
innerData2.Fields.AddOrUpdateValue(SystemFieldDefinitionConstants.Email, "email2@domain.com");
//Create a sample static group and assign multi field value to it:
var sampleGroup = new StaticGroup(sampleGroupFieldTemplate.SystemId,this.UniqueString());
sampleGroup.Fields.AddOrUpdateValue(multiFieldDefinition.Id,new [] {innerData1, innerData2});
_groupService.Create(sampleGroup);
//Retrieve the group we've created and extract the multifield value
var savedGroup = _groupService.Get<StaticGroup>(sampleGroup.SystemId);
savedGroup.Fields.TryGetValue(multiFieldDefinition.Id, out var multiFieldValue);
//Retrieve values
var list = IList<MultiFieldItem>(multiFieldValue);
if(list != null)
{
var count = list.Count; // should be 2
var name1 = list.First().Fields.GetValue<string>(SystemFieldDefinitionConstants.Name, CultureInfo.CurrentCulture); // should be "name1"
var email1 = list.First().Fields.GetValue<string>(SystemFieldDefinitionConstants.Email) //Should be "email1@domain.com"
var name2 = list.Last().Fields.GetValue<string>(SystemFieldDefinitionConstants.Name, CultureInfo.CurrentCulture); // should be "name2"
var email2 = list.Last().Fields.GetValue<string>(SystemFieldDefinitionConstants.Email) //Should be "email2@domain.com"
}
}
MultiField data is stored in the same tables as normal fields (<EntityType>FieldData). Based on the ChildOwnerId and ChildIndex columns, we can group the individual values into objects. In the example below, we have three slides of the MultiField with the Id Slider. ChildIndex with the value 0 belongs to the first slide, and so on.
