Map custom member fields to contacts

When a visitor registers as a member, the system maps the member’s email address to the corresponding contact by default. You can customize this behavior by creating and registering an IMemberToContactMapper implementation to map any custom member fields to contact fields, for example their first or last name.

We recommend extending the default implementation of the service using the decorator customization pattern. First map your custom member fields to the contact (without setting the ContactInfo object), and then call the default implementation to map the email address and save the contact details.

The default service implementation maps data from members to contacts only. Any changes to the member object will not be saved.

Implement the custom member field mapper

Start by preparing a separate project for custom classes in your Xperience solution:

  1. Open your project’s solution in Visual Studio.
  2. Add a custom assembly (Class Library project) with class discovery enabled to the solution, or re-use an existing assembly.
  3. Reference the project from your Xperience website project.

Continue by implementing the custom class:

  1. Create a new custom class. For example, name the class CustomMemberToContactMapper.
  2. Make the class implement the IMemberToContactMapper interface.
  3. Implement the Map method using the Decorator pattern.
  4. Register the implementation using the RegisterImplementation assembly attribute.
C#Custom member fields to contact mapper example


using CMS;
using CMS.ContactManagement;
using CMS.Membership;

using Kentico.OnlineMarketing.Web.Mvc;
using Kentico.OnlineMarketing.Web.Mvc.MemberRegistration.Mappers;

[assembly: RegisterImplementation(typeof(IMemberToContactMapper), typeof(CustomMemberToContactMapper))]
class CustomMemberToContactMapper : IMemberToContactMapper
{
    private readonly IMemberToContactMapper memberToContactMapper;

    public CustomMemberToContactMapper(IMemberToContactMapper memberToContactMapper)
    {
        this.memberToContactMapper = memberToContactMapper;
    }

    /// Maps the member's first and last name to the contact's.
    public void Map(MemberInfo member, ContactInfo contact)
    {
        contact.ContactFirstName = member.GetValue<string>("FirstName", null);
        contact.ContactLastName = member.GetValue<string>("LastName", null);

        memberToContactMapper.Map(member, contact);

    }
}