Searching contacts by email with xConnect in Sitecore 9

xConnect has been introduced in Sitecore 9 to simplify communication between applications and xDB. It has really cool API and can help create true omnichannel gathering / reading of data. Therefore you are not finally storing only “web” interactions but theoretically anything for each contact.

By default xConnect has lots of features which support GDPR adoption in solutions and help developers make solutions GDPR complaint. Sneak peak – Some follow up blog posts will come in near future šŸ˜‰

Problem

One of the xConnect features is contact and interaction facets which support storing Personal Identifiable Information (PII). There is a small discrepancy between Personal Indentifiable Information (term mostly used in US ) and Personal Data (term used in EU and especially for GDPR). In short, Personal Data term covers broader range of data / information types so it is more restrictive.

When you try to search contacts by email or other sensitive information with xConnect, OOTB it won’t work. This is expected behavior but a bit confusing for developers.

Sample code:

using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    IAsyncQueryable<Sitecore.XConnect.Contact> queryable = client.Contacts
                        .Where(c => c.GetFacet<EmailAddressList>(EmailAddressList.DefaultFacetKey).PreferredEmail.SmtpAddress == "some@emailaddress.com")
                        .WithExpandOptions(new ContactExpandOptions(EmailAddressList.DefaultFacetKey));

                    var enumerator = queryable.GetBatchEnumeratorSync(10);

                    while (enumerator.MoveNext())
                    {
                        foreach (var contact in enumerator.Current)
                        {
                            var mail = contact.Emails().PreferredEmail.SmtpAddress;
                        }
                    }
                }
                catch (XdbExecutionException ex)
                {
                    // Handle exception
                }
            } 

 

This won’t give you expected results and enumerator will be empty.

Reason is that these facets are decorated with attribute [PIISensitive]. This means that they are storing personal sensitive data. If technically speaking, this means that they are not indexed and therefore not searchable.

Facet definition for email looks like this:

[PIISensitive]
 public string SmtpAddress { get; set; }

Solution

In order to enable searching by these sensitive information, you need to enable indexing of these kind of facets (decorated with [PIISensitive] facet).

You need to set “true” in IndexPIISensitiveData setting in xConnect SearchIndexer settings.

You need to rebuild xDB search index afterwards to make changes visible and therefore email facet searchable.

More information about this can be found in awesome official xConnect documentation.

This solution is obviously not usable in scenario where you want to be complaint with GDPR so think twice about this before doing it! You need to have consent from contacts for this kind of processing.

I am still searching for solution that wouldn’t violate this.

One possible option would be to only search in email facet for contacts that gave you consent for processing BUT how would you make indexing work then? Some contact facets indexed, some not… Really complex change of the guts of xConnect and indexing. Also how about contacts that won’t give you consent? They will be invisible during searching…

 

Happy coding!

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s