How to Update Contacts in Sitecore xDB

This is a quick, short and sharp post covering how to add data to Contacts in xDB, specifically to the Facets and Elements that come with Sitecore out-of-the-box. This post is to provide additional info to that goes with recent posts covering how to identify and merge Contacts in xDB and how to extend xDB to store custom data.

To give an idea how this can be applied in the real world I am using the following example for this series of blogs. We have a website where Users can order products online. In the purchase path there is a form to collect base information so we can fulfill their order; name, contact info, address, email address etc. We will also use this information update users’ Contacts with the information to build a picture of who the site’s users are. This update will occur when the user clicks Next on the form – save us running the risk of them not making it to the end of the purchase path.

Updating Sitecore Contacts

Writing data from our Order form to the Contact’s out-of-the-box Facets is actually pretty simple. Get the Contact (I’ve covered an robust way to do this) and call GetFacet using the Interface of the type passing in the name of the Facet. Setting some personal details is as below. Note: its your call if you want to overwrite the data each time.

IContactPersonalInfo contactPersonalInfo = contact.GetFacet<IContactPersonalInfo>("Personal");
if (string.IsNullOrEmpty(contactPersonalInfo.FirstName))
contactPersonalInfo.FirstName = OrderFormModel.FirstName;
if (string.IsNullOrEmpty(contactPersonalInfo.Surname))
contactPersonalInfo.Surname = OrderFormModel.LastName;
if (string.IsNullOrEmpty(contactPersonalInfo.JobTitle))
contactPersonalInfo.JobTitle = OrderFormModel.JobType;

Writing to MongoDB Elements is that simple, but it gets more complex when dealing with lists of data like Collections. Lets use an example. In xDB Addresses Facet consists of a Collection for the addresses and an Attribute to define a Preferred Address. The same the Email Addresses Facet. So in our example we want an entry for our Shipping Address.

With the Addresses Facet in hand, create the entry for Shipping Address (if its not present already). If the entry doesn’t exist an error will occur. On the entry set the values to the address details the user entered in the form. I found it to be good practice to set the preferred entry of Facet to be the one we are creating if one has not already been set.

IContactAddresses contactAddresses = contact.GetFacet<IContactAddresses>("Addresses");
if (string.IsNullOrEmpty(contactAddresses.Preferred))
contactAddresses.Preferred = "Shipping Address";
if (!contactAddresses.Entries.Contains("Shipping Address"))
contactAddresses.Entries.Create("Shipping Address");
contactAddresses.Entries["Shipping Address"].StreetLine1 = orderFormModel.AddressLine1;
contactAddresses.Entries["Shipping Address"].StreetLine2 = orderFormModel.AddressLine2;
contactAddresses.Entries["Shipping Address"].City = orderFormModel.CityTown;
contactAddresses.Entries["Shipping Address"].StateProvince = orderFormModel.State.Value;
contactAddresses.Entries["Shipping Address"].Country = orderFormModel.Country.Value;
contactAddresses.Entries["Shipping Address"].PostalCode = OrderFormModel.PostalCode;

Once the Contact has been updated you can leave the write to MongoDB to occur when the user’s session ends or call SaveAndReleaseContactToXdb() to do it immediately.

ContactManager contactManager = Sitecore.Configuration.Factory.CreateObject("tracking/contactManager", true) as ContactManager;

And that’s it!

Adding data to the default Facets of an xDB Contact. It’s pretty simple really. As I mentioned, be sure to check out the two related posts to learn how to extend xDB to store custom data, how to handle identifying users and how to persist data when merging two Contacts together.

3 thoughts on “How to Update Contacts in Sitecore xDB

  1. Thanks for this post! This helped me get my head around the Entries structures used on the contact. I tried using the contact manager to save and release, but it didn’t seem to show up in xDB any faster. Is there a more direct way via the API to send the new data to update the contact in the DB?


    • Hi Jason, the data is stored in session until the users session ends for scalability. The most effective way to get the data in xDB immediately is to call Session.Abandon(). It’s fine for using in a Dev environment for testing etc but wouldn’t be suitable for production. Let me know if I can help out any more


  2. Pingback: How to Identify and Merge Contacts in Sitecore xDB | Exercising Sitecore

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s