Search contacts and Commerce interactions in the xDB index

Current version: 10.1

Using the xConnect Search API, you can search contacts and Commerce interactions to produce, for example, a list of customers who have placed orders or a list of all product purchased by a customer.

Note

You can find a blacklist of all models that are not indexed by default. These models have the DoNotIndexAttribute type. This list is contained in the Sitecore.Commerce.Connect.XConnect.Models.json file that is deployed with a custom model.

This topic provides examples of how you can use the xConnect Search API to query contacts based on Commerce events.

Note

On Azure, you can put the nested loops in the following examples into a single LINQ statement.

Return a list of customers who have placed orders

The following code example returns a list of contacts who have placed orders.

RequestResponse
using (var client = new XConnectClient(cfg))
            {
                var query = client.Contacts.Where(c => c.Interactions
                    .Any(i => i.Events.OfType<VisitorOrderCreatedGoal>()
                        .Any(e => e.Order.CartLines.Any(l => l.Product.ProductName == "Habitat Sentinel Touchscreen Thermostat"))));

                var enumerator = await query.GetBatchEnumerator(20);
                List<IEntityReference<Contact>> contactsWhoPlacedOrders = new List<IEntityReference<Contact>>();

                while (await enumerator.MoveNext())
                {
                    var contacts = enumerator.Current;
                    contactsWhoPlacedOrders.AddRange(contacts);
                }
            }

Return a list of all products purchased by a customer

The following code example returns a list of products that have been purchased by a specific customer.

RequestResponse
using (var client = new XConnectClient(cfg))
            {
                var contactId = Guid.Parse("1822ef81-3016-0000-0000-05a9dc69662e");
                var query = client.Interactions
                    .Where(i => i.Contact.Id == contactId
                        && i.Events.OfType<VisitorOrderCreatedGoal>().Any());
                var enumerator = await query.GetBatchEnumerator();

                while (await enumerator.MoveNext())
                {
                    var interactions = enumerator.Current;
                    var productList = interactions.Where(i => i.Events.OfType<VisitorOrderCreatedGoal>().Any())
                        .SelectMany(i => i.Events.OfType<VisitorOrderCreatedGoal>())
                            .SelectMany(e => e.Order.CartLines)
                            .Select(cl=>cl.Product).ToList();
                }
            }

Return a list of all products viewed by a customer

The following code example returns a list of all the products that have been viewed by an individual customer.

RequestResponse
using (var client = new XConnectClient(cfg))
            {
                var query = client.Interactions
                    .Where(i => i.Contact.Id == Guid.Parse("1822ef81-3016-0000-0000-05a9dc69662e")
                        && i.Events.OfType<VisitedProductDetailsPageEvent>().Any());
                var enumerator = await query.GetBatchEnumerator();

                while (await enumerator.MoveNext())
                {
                    var interactions = enumerator.Current;
                    var visitedProductsListIds = interactions
                        .SelectMany(i => i.Events.OfType<VisitedProductDetailsPageEvent>())
                        .Select(e => e.ProductId).ToList();
                }
            }

Return a list of all categories visited by a customer

The following code example returns a list of all the categories that a customer visited.

RequestResponse
var cfg = GetXConnectClientConfiguration();

            using (var client = new XConnectClient(cfg))
            {
                var query = client.Interactions
                    .Where(i => i.Contact.Id == Guid.Parse("1822ef81-3016-0000-0000-05a9dc69662e")
                        && i.Events.OfType<VisitedCategoryPageEvent>().Any());
                var enumerator = await query.GetBatchEnumerator();

                while (await enumerator.MoveNext())
                {
                    var interactions = enumerator.Current;
                    var visitedCategoryListIds = interactions
                        .SelectMany(i => i.Events.OfType<VisitedCategoryPageEvent>())
                        .Select(e => e.CategoryId).ToList();
                }
            }

Return a list of customers who have abandoned their carts

The following code example returns a list of customers who have abandoned their carts.

RequestResponse
var cfg = GetXConnectClientConfiguration();

            using (var client = new XConnectClient(cfg))
            {
                var query = client.Contacts.Where(c => c.Interactions
                    .Any(i => i.Events.OfType<AbandonedCartPageEvent>().Any()));
                var enumerator = await query.GetBatchEnumerator(20);
                List<IEntityReference<Contact>> contactsWithAbandonedCart = new List<IEntityReference<Contact>>();

                while (await enumerator.MoveNext())
                {
                    var contacts = enumerator.Current;
                    contactsWithAbandonedCart.AddRange(contacts);
                }
            }

Do you have some feedback for us?

If you have suggestions for improving this article,