---
title: Contacts
---

> **Documentation indexes**
> The indexes below list current page URLs with titles and descriptions. Links on this page point to the Markdown versions of the linked pages.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

List of examples:

## Dependency injection

### Initialize required services

```csharp
// Initializes all services and provider classes used within
// the API examples on this page using dependency injection.
public class ContactsServices(
    IInfoProvider<ContactInfo> contactInfoProvider,
    IContactsBulkDeletionService contactDeletionService)
{
}
```

[> Back to list of examples](#toc)

## Contacts

### Get the current contact

```csharp
// Gets the contact related to the currently processed request
ContactInfo currentContact = ContactManagementContext.CurrentContact;
```

[> Back to list of examples](#toc)

### Create contacts

```csharp
// Creates a new contact object
ContactInfo newContact = new ContactInfo()
{
    ContactLastName = "Smith",
    ContactFirstName = "John",

    // Enables activity tracking for the new contact
    ContactMonitored = true
};

// Saves the contact to the database
contactInfoProvider.Set(newContact);
```

[> Back to list of examples](#toc)

### Update contacts

```csharp
// Gets the first contact whose last name is 'Smith'
ContactInfo? updateContact = contactInfoProvider
    .Get()
    .WhereEquals("ContactLastName", "Smith")
    .TopN(1)
    .FirstOrDefault();

if (updateContact != null)
{
    // Updates the contact's properties
    updateContact.ContactCompanyName = "Company Inc.";

    // Saves the updated contact to the database
    contactInfoProvider.Set(updateContact);
}
```

[> Back to list of examples](#toc)

### Update multiple contacts

```csharp
// Gets all contacts whose last name is 'Smith'
var contacts = contactInfoProvider.Get()
                                  .WhereEquals("ContactLastName", "Smith");

// Loops through individual contacts
foreach (ContactInfo contact in contacts)
{
    // Updates the properties of the contact
    contact.ContactCompanyName = "Company Inc.";

    // Saves the updated contact to the database
    contactInfoProvider.Set(contact);
}
```

[> Back to list of examples](#toc)

### Delete contacts

```csharp
// Gets the first contact whose last name is 'Smith'
ContactInfo? deleteContact = contactInfoProvider
    .Get()
    .WhereEquals("ContactLastName", "Smith")
    .TopN(1)
    .FirstOrDefault();

if (deleteContact != null)
{
    // Deletes the contact
    contactInfoProvider.Delete(deleteContact);
}
```

[> Back to list of examples](#toc)

### Delete contacts in bulk

```csharp
var dateOneWeekAgo = DateTime.Now.AddDays(-7);

// WhereCondition that selects contacts that are at least 7 days old and have an empty email address
var condition = new WhereCondition()
                .WhereLessThan(nameof(ContactInfo.ContactCreated), dateOneWeekAgo)
                .And()
                .WhereEmpty(nameof(ContactInfo.ContactEmail));

// Deletes contacts matching the specified condition
// Doesn't specify a batch size, which means that contacts are deleted in batches of 1000 (automatically repeats)
await contactDeletionService.BulkDelete(condition);
```

[> Back to list of examples](#toc)
