Multi-node xDB Facet

Multi-node xDB Facet

Creating an IElementDictionary Facet

In my earlier post, I described how to make a single node facet for xDB. This was a simple facet that tracks information about a contact like name, company, location, etc... But now lets take the facet and push it further with a facet that is an array and allows more than one entity. An example of this data is Products Viewed or Videos Watched. The user would be able to do many of these and we need to track them all for later reporting.

There are two types of facet arrays in Sitecore. One is called IElementCollection and the other is IElementDictionary

IElementCollection

The IElementCollection allows for one to many elements to be added to the xDB Contact document. When you add a new element to an IElementCollection it is added as an array. So to get one, you have to call for it by its place in the index, not by name. Each element gets an incrementing integer as its node as you can see in the image below.

IElementDictionary

The IElementDictionary is similar to the IElementCollection, but the dictionary using a string field as its identifier. This allows you to get the node by name when you want a single node. When looking at the current xDB contact element, the IElementDictionary is what is used the most.

Creating the data object

The first item we need to create is the interface to the data object. This interface need to inherit from IElement and IValidatable.

Create the container to hold the data object

Create a new interface that inherits from IFacet, IElement and IValidatable. Also create a new IElementDictionary property that has the interface type of the object you created above. The name of the the property is up to you. The consistent name is Entries throughout the Contact object, but use whatever name works for your situation.

Create a concrete data object

Create a serializable concrete implementation of the IElement data object you created above. Make sure the class inherits from Element, IElementCustomerLookup, IElement and IValidatable.

Create a concrete entities object

Create a serializable concrete implementation of the entities container. Make sure it inherits from Facet, IContactCustomerLookups, IFacet, IElement and IValidatable. And also implements EnsureDictionary in the default constructor.

The config

Tie it together

At this point we have all of our interfaces and classes to create multi-node facets in xDB. Now we just need a config to tie it all together. It will look like this.

The elements section defines for each interface, what concrete class defines it. Its very much like dependency injection.

The entities > contact > facets defines what class you created above will be created as a facet in xDB.

Finally the, lets add a record to our xDB. All we do it call for the CompanyInfo facet via the name and the interface, then fill the data. Tada.

Now the data is in and ready for us to process and use it for the user's experience. With the 1 to many records we can add to the customer's facet, this is a great use for products the customer has viewed, videos the customer has watched, product pdf the customer has downloaded. There are a million use cases for the live faceted data.

Next steps

Next step, processing this data.