Skip to main content

Create custom reference data types

Abstract

How to create custom reference data types.

You can create additional reference data types as needed. To create a reference data type:

  1. Create a custom common data type. Common data is culture independent and does not require translation. Ensure that the class is marked as [Serializable]:

    namespace Documentation
    {
        [Serializable]
        public class AiportCommonData
        {
            public int NumberOfRunways { get; set; }
        }
    }
    
  2. Create a custom culture data type. Ensure that the class is marked as [Serializable]:

    namespace Documentation
    {
        [Serializable]
        public class AirportCultureData
        {
            public string AirportName { get; set; }
        }
    }
    
  3. Choose a definition type name. It is recommended that you store definition type names in a central location. For example, Sitecore’s default definition type names are stored under Sitecore.Marketing.Operations.Xdb.ReferenceData.Model.Definitions.DefinitionTypeNames.

  4. Use custom definition type as shown. If the type does not exist, it will be created when the EnsureDefinitionTypeAsync() method is called:

    using Sitecore.DependencyInjection;
    using Sitecore.Xdb.ReferenceData.Client;
    using Sitecore.Marketing.Operations.Xdb.ReferenceData.Model;
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Sitecore.Xdb.ReferenceData.Core;
    
    namespace Documentation
    {
        public class RefDataCustomDefinitionData
        {
            public async void Example()
            {
                var client = ServiceLocator.ServiceProvider.GetService(typeof(IReferenceDataClient)) as IReferenceDataClient;
    
                DefinitionTypeKey definitionType = await client.EnsureDefinitionTypeAsync("Airport Codes"); // Do not hardcode defintion type name like this
    
                var definitionKey = new DefinitionKey("LHR", definitionType, 1);
                var definition = new Definition<AiportCommonData, AirportCultureData>(definitionKey)
                {
                    IsActive = true,
                    CommonData = new AiportCommonData
                    {
                        NumberOfRunways = 5
                    }
                };
    
                definition.CultureData.Add(new CultureInfo("en"), new AirportCultureData() { AirportName = "Heathrow Airport" });
                definition.CultureData.Add(new CultureInfo("dk"), new AirportCultureData() { AirportName = "Danish Heathrow Airport" });
    
                await client.SaveAsync(definition);
            }
        }
    
        [Serializable]
        public class AirportCultureData
        {
            public string AirportName { get; set; }
        }
    
        [Serializable]
        public class AiportCommonData
        {
            public int NumberOfRunways { get; set; }
        }
    }