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:
- Open your project’s solution in Visual Studio.
- Add a custom assembly (Class Library project) with class discovery enabled to the solution, or re-use an existing assembly.
- Reference the project from your Xperience website project.
Continue by implementing the custom class:
- Create a new custom class. For example, name the class
CustomMemberToContactMapper
. - Make the class implement the
IMemberToContactMapper
interface. - Implement the
Map
method using the Decorator pattern. - Register the implementation using the
RegisterImplementation
assembly attribute.
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);
}
}