---
title: "Search contacts and Commerce interactions in the xDB index"
slug: "search-contacts-and-commerce-interactions-in-the-xdb-index"
abstract: "How to query commerce models and properties in the xDB index."
---
# Search contacts and Commerce interactions in the xDB index

Using the xConnect Search API, you can [search contacts and Commerce interactions](https://doc.sitecore.com/xp/en/developers/101/sitecore-experience-platform/work-with-search.html) 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](https://doc.sitecore.com/xp/en/developers/101/sitecore-experience-platform/xconnect-service.html)* type. This list is contained in the `Sitecore.Commerce.Connect.XConnect.Models.json` file that is [deployed with a custom model](https://doc.sitecore.com/xp/en/developers/101/sitecore-experience-platform/deploy-a-custom-model.html).

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.

<details>
<summary><h2>Return a list of customers who have placed orders</h2></summary>

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

```csharp
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);
                }
            }

```

</details>

<details>
<summary><h2>Return a list of all products purchased by a customer</h2></summary>

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

```csharp
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();
                }
            }

```

</details>

<details>
<summary><h2>Return a list of all products viewed by a customer</h2></summary>

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

```csharp
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();
                }
            }

```

</details>

<details>
<summary><h2>Return a list of all categories visited by a customer</h2></summary>

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

```csharp
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();
                }
            }

```

</details>

<details>
<summary><h2>Return a list of customers who have abandoned their carts</h2></summary>

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

```csharp
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);
                }
            }

```

</details>