Search contacts
This topic demonstrates how to query contacts. Each example uses the client.GetBatchEnumerator() / client.GetBatchEnumeratorSync() to paginate results. Examples that rely on joins have an equivalent example that uses the InteractionsCache facet for search providers that do not support joins.
Refer to the list of supported methods for xConnect search.
Search contacts by ID
Search contacts by ID
Search by ID is not supported. If you know the contact’s ID, you can get the contact from the collection database.
Search contacts by identifer source and type
Search contacts by identifer source and type
Identifiers are stored in an encrypted field in the xDB Collection database and are not indexed. You can search by identifier source or type, or use an identifier and identifier source to get a contact from the xDB Collection database.
Search contacts by identifier source
The following example demonstrates how to get all contacts with at least one identifier where the value of the Source property is twitter. Results are returned in batches of 10. For each contact, a new identifier with the source tweeter is added and the old identifier is removed (identifiers cannot be edited). Operations are submitted to xConnect in batches of approximately 200.
Search contacts by identifier type
In the following example, all contacts with at least one known identifier are returned.
Search by by facet property value
Search by by facet property value
In the following example, all contacts with the job title Programmer Writer are returned. To include facets in the results, you must use the .WithExpandOptions method as shown.
If you are not indexing PII sensitive data, you cannot search by facets or facet properties that are marked PII sensitive. For example, a contact’s first name and last name are marked as PII sensitive.
Search by existence of contact facet
It is not possible to search for null values. In order to determine whether or not a facet has been set, you should search for the property that is most likely to have a value. For example:
-
You must pass a preferred e-mail address into the constructor of the
EmailAddressListclass - therefore, you can rely on thePreferredEmail.SmtpAddressproperty being populated. -
The
PersonalInformationclass does not have any mandatory properties - therefore, you must check the property that is most likely to have been set.
In the following example, any contact with the EmailAddressList and PersonalInformation facets is returned. This example relies on being able to search for the contact’s first name, which is PII sensitive data:
Search by nested facet property value
The following example returns all contacts that have any Bristol-based address. Expand options are used to return the AddressList facet with the results.
You must check the AddressList.Preferred property and the AddressList.Others property.
Using facet extension methods
You can use facet extension methods in the context of a query. The following example demonstrates how to retrieve the AddressList facet using the .GetFacet<AddressList> method and the .Addresses() method:
Search by contact behavior
Search by contact behavior
Joins are only possible if the search provider supports joins. Use the example that suits your requirements.
Contact and interaction search can be combined to return contacts whose interactions match particular criteria. To search interactions, use interaction search.
The query will return all contacts with interactions that match the query. However, interactions that are returned using expand options will not match the query parameters.
Search by interaction date
The following examples demonstrate how to retrieve contacts that have an interaction that is older than five days. In addition, expand options are used to return the contact’s top 20 interactions - these interactions will not necessarily match the query parameters.
With joins
The following example uses joins to get any contact with one or more matching interactions.
Without joins
The following example uses the InteractionsCache facet to get contacts with one or more matching interactions.
Search by interaction facets
The following examples demonstrate how to retrieve contacts that have an interaction with a specific referrer.
With joins
The following example uses joins to get contacts with one or more matching interactions.
Without joins
The following example uses the InteractionsCache facet to get contacts with one or more matching interactions.
Search by interaction events
The following example demonstrates how to search for contacts that have triggered an event that matches the following criteria:
-
Where the event type is
Goal -
Where the event definition ID is
{29408b2d-52b6-4f39-96ca-039cd96f4624}
With joins
The following example uses joins to get contacts with one or more matching interactions.
Without joins
The following example uses the InteractionsCache facet and is supported by all search providers, including providers that do not support joins:
Search by recent activity
Search by recent activity
EngagementMeasures and KeyBehaviorCache are calculated facets that store information about a contact’s recent behavior. Querying these facets does not require joins as they both belong to the contact. The following examples demonstrate how to use the EngagementMeasures and KeyBehaviorCache to return contacts whose recent behavior match a specific set of criteria.
Engagement measures
The following query returns all contacts with a recent interaction with a duration of over thirty seconds:
Key behavior cache
The following query returns all contacts with a recent interaction that took place at a venue with the definition ID 951fb783-5959-49a4-a1f3-ced3453725a4.
Results and pagination
Results and pagination
The .GetBatchEnumerator() or .GetBatchEnumeratorSync() extension methods are the recommended way to return results from a query. See the overview of pagination for more information.
For each method, the maximum size of a single batch is hardcoded to Sitecore.XConnect.SearchExtensions.DefaultBatchSize, which is set to 1000. This value is currently not configurable.
|
Asynchronous | |
|---|---|
|
Method |
Notes |
|
|
Recommended way of paginating results. |
|
|
Can be used with |
|
|
Can be used with |
|
Synchronous | |
|---|---|
|
Method |
Notes |
|
|
Recommended way of paginating results. |
|
|
Can be used with |
The following example demonstrates how to use each method to return results:
Ordering results
You can orders results by a contact’s facets or properties. The following example demonstrates how to order contacts by the MostRecentInteractionStartDateTime property on the EngagementMeasures facet:
You cannot order by the properties of a list. For example, you cannot order a list of contacts by the StartDateTime of its interactions.
Expand options
Expand options
Use the .WithExpandOptions() method to specify which facets and related interactions to return with each contact. These expand options are identical to the ones that you use when retrieving a contact by ID or identifier. If you do not specify any expand options, no contact facets or related interactions will be returned.
In the following example we are:
-
Retrieving one contact facet
-
Retrieving one interaction facet
-
Retrieving a maximum of 3 interactions per contact
Specifying a related interaction limit of 1 will ensure that only the latest interaction is returned.
Related interactions are not affected by the query
The following query returns all contacts with at least one interaction that has the WebVisit facet. The query also returns the contact’s top 30 interactions:
The Interactions collection is not affected by the query. The query returns all contacts with at least one matching contact from any point in time. However, the interaction that resulted in a match will not necessarily be present in every contact’s Interactions property.
Count contacts
Count contacts
Use the following method to return a count of all matching contacts without returning any contact data: