Extending xDB – Custom Contact Facets

If you have a website, chances are you are using more than one system. And if you have user data spread across systems, you may want to use data from one system in another. If you have a global unique identifier for your user, this is an easy task. But, if you do not have that you’ll need to maintain a relationship.

In this post, I’ll review how you can extend Sitecore’s xDB to add a new contact facet that will let you store external system identifiers.

The first step is to create the necessary contracts. I’ll be creating two “IExternalSystemIdentifier” – for a identifier and “IContactExternalIdentifiers” – for a list of identifiers

 

  public interface IExternalSystemIdentifier : IElement, IValidatable
    {
        string Identifier { get; set; }

    }

  public interface IContactExternalIdentifiers : IFacet, IElement, IValidatable
    {
        IElementDictionary<IExternalSystemIdentifier> Entries { get; }

        string Preferred { get; set; }
    }

 

The next step is to implement these contracts

 

    internal class ExternalSystemIdentifier : Element, IExternalSystemIdentifier
    {
        public string Identifier
        {
            get
            {
                return GetAttribute<string>("ExternalIdentifier");
            }
            set
            {
                SetAttribute("ExternalIdentifier", value);
            }
        }
        public ExternalSystemIdentifier()
        {
            EnsureAttribute<string>("ExternalIdentifier");
        }
    }

For IContactExternalIdentifiers

    [Serializable]
    public class ContactExternalIdentifiers : Facet, IContactExternalIdentifiers
    {

        public IElementDictionary<IExternalSystemIdentifier> Entries => GetDictionary<IExternalSystemIdentifier>("Entries");

        public string Preferred
        {
            get
            {
                return GetAttribute<string>("Preferred");
            }
            set
            {
                SetAttribute("Preferred", value);
            }
        }

        public ContactExternalIdentifiers()
        {
            EnsureAttribute<string>("Preferred");
            EnsureDictionary<IExternalSystemIdentifier>("Entries");
        }
    }

 

Now to set up the configuration files

 

<sitecore>
 <model>
      <elements>
        <element interface="Sandbox.Extensions.Entities.IContactExternalIdentifiers, Sandbox" implementation="Sandbox.Extensions.Entities.ContactExternalIdentifiers, Sandbox"/>
        <element interface="Sandbox.Extensions.Entities.IExternalSystemIdentifier, Sandbox" implementation="Sandbox.Extensions.Entities.ExternalSystemIdentifier, Sandbox"/>
      </elements>
      <entities>
        <contact>
          <facets>
            <facet name="External System Identifiers" contract="Sandbox.Extensions.Entities.IContactExternalIdentifiers, Sandbox"/>
          </facets>
        </contact>
      </entities>
    </model>
</sitecore>

 

Finally, you can use this facet to save or read data from, using the following code

 

// write data    
var externalSystemIdentifierFacet = contact.GetFacet<IContactExternalIdentifiers>(Constants.ContactFacetNames.ContactExternalSystemIdentifiers);
            if (externalSystemIdentifierFacet != null)
            {
                var identifierFacet = externalSystemIdentifierFacet.Entries.Contains("External System 1")
                    ? externalSystemIdentifierFacet.Entries["External System 1"]
                    : externalSystemIdentifierFacet.Entries.Create("External System 1");
                identifierFacet.Identifier = "Add the identifier here";
            }

//read data

 var externalSystemIdentifierFacet = contact.GetFacet<IContactExternalIdentifiers>(Constants.ContactFacetNames.ContactExternalSystemIdentifiers);
            return externalSystemIdentifierFacet == null ||
                   !externalSystemIdentifierFacet.Entries.Contains("External System 1")
                ? "No Data Provided"
                : externalSystemIdentifierFacet.Entries["External System 1"].Identifier;

 

For more information on custom facets please review Create a custom contact facet

Extending xDB – Custom Contact Facets
Tagged on:

One thought on “Extending xDB – Custom Contact Facets

Leave a Reply

Your email address will not be published. Required fields are marked *