---
title: Contacts
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [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:

## Contacts

### Creating a contact

```csharp

// Creates a new contact object for the current site
ContactInfo newContact = new ContactInfo()
{
    ContactLastName = "Smith",
    ContactFirstName = "John",
    ContactSiteID = SiteContext.CurrentSiteID,
    ContactIsAnonymous = true
};

// Saves the contact to the database
ContactInfoProvider.SetContactInfo(newContact);

```

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

### Updating a contact

```csharp

// Gets the first contact on the current site whose last name is 'Smith'
ContactInfo updateContact = ContactInfoProvider.GetContacts()
                                    .WhereEquals("ContactLastName", "Smith")
                                    .WhereEquals("ContactSiteID", SiteContext.CurrentSiteID)
                                    .FirstObject;

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

    // Saves the updated contact to the database
    ContactInfoProvider.SetContactInfo(updateContact);
}

```

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

### Updating multiple contacts

```csharp

// Gets all contacts on the current site whose last name is 'Smith'
var contacts = ContactInfoProvider.GetContacts()
                                    .WhereEquals("ContactLastName", "Smith")
                                    .WhereEquals("ContactSiteID", SiteContext.CurrentSiteID);

// 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.SetContactInfo(contact);
}

```

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

### Linking contacts with user accounts

```csharp

// Gets the first contact on the current site whose last name is 'Smith'
ContactInfo contact = ContactInfoProvider.GetContacts()
                                    .WhereEquals("ContactLastName", "Smith")
                                    .WhereEquals("ContactSiteID", SiteContext.CurrentSiteID)
                                    .FirstObject;

if (contact != null)
{
    // Links the contact with the current user
    ContactMembershipInfoProvider.SetRelationship(
        MembershipContext.AuthenticatedUser.UserID,
        MemberTypeEnum.CmsUser,
        contact.ContactID,
        contact.ContactID,
        false);
}

```

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

### Removing user accounts from contacts

```csharp

// Gets the first contact on the current site whose last name is 'Smith'
ContactInfo contact = ContactInfoProvider.GetContacts()
                                    .WhereEquals("ContactLastName", "Smith")
                                    .WhereEquals("ContactSiteID", SiteContext.CurrentSiteID)
                                    .FirstObject;

if (contact != null)
{
    // Gets the relationship between the contact and the current user
    ContactMembershipInfo contactMembership = ContactMembershipInfoProvider.GetMembershipInfo(
        contact.ContactID,
        contact.ContactID,
        MembershipContext.AuthenticatedUser.UserID,
        MemberTypeEnum.CmsUser);

    // Deletes the contact-user relationship
    ContactMembershipInfoProvider.DeleteRelationship(contactMembership.MembershipID);
}

```

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

### Assigning IP addresses to contacts

```csharp

// Gets the first contact on the current site whose last name is 'Smith'
ContactInfo contact = ContactInfoProvider.GetContacts()
                                    .WhereEquals("ContactLastName", "Smith")
                                    .WhereEquals("ContactSiteID", SiteContext.CurrentSiteID)
                                    .FirstObject;

if (contact != null)
{
    // Creates a new IP address and assigns it to the contact
    IPInfo newIP = new IPInfo()
    {
        IPAddress = "127.0.0.1",
        IPOriginalContactID = contact.ContactID,
        IPActiveContactID = contact.ContactID
    };

    // Saves the IP address to the database
    IPInfoProvider.SetIPInfo(newIP);
}

```

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

### Removing IP addresses from contacts

```csharp

// Gets the first contact on the current site whose last name is 'Smith'
ContactInfo contact = ContactInfoProvider.GetContacts()
                                    .WhereEquals("ContactLastName", "Smith")
                                    .WhereEquals("ContactSiteID", SiteContext.CurrentSiteID)
                                    .FirstObject;

if (contact != null)
{
    // Gets all IP addresses assigned to the contact
    var deleteIPs = IPInfoProvider.GetIps().WhereEquals("IPOriginalContactID", contact.ContactID);

    // Loops through individual IP adresses
    foreach (IPInfo ipAddress in deleteIPs)
    {
        // Deletes the IP address
        IPInfoProvider.DeleteIPInfo(ipAddress);
    }
}

```

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

### Assigning user agents to contacts

```csharp

// Gets the first contact on the current site whose last name is 'Smith'
ContactInfo contact = ContactInfoProvider.GetContacts()
                                    .WhereEquals("ContactLastName", "Smith")
                                    .WhereEquals("ContactSiteID", SiteContext.CurrentSiteID)
                                    .FirstObject;

if (contact != null)
{
    // Creates a new agent object and assigns it to the contact
    UserAgentInfo agentInfo = new UserAgentInfo()
    {
        UserAgentActiveContactID = contact.ContactID,
        UserAgentOriginalContactID = contact.ContactID,
        UserAgentString = "Custom User Agent"
    };

    // Saves the user agent object to the database
    UserAgentInfoProvider.SetUserAgentInfo(agentInfo);
}

```

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

### Removing user agents from contacts

```csharp

// Gets the first contact on the current site whose last name is 'Smith'
ContactInfo contact = ContactInfoProvider.GetContacts()
                                    .WhereEquals("ContactLastName", "Smith")
                                    .WhereEquals("ContactSiteID", SiteContext.CurrentSiteID)
                                    .FirstObject;

if (contact != null)
{
    // Gets all user agents assigned to the contact
    var deleteAgents = UserAgentInfoProvider.GetUserAgents().WhereEquals("UserAgentOriginalContactID", contact.ContactID);

    // Loops through individual user agents
    foreach (UserAgentInfo agent in deleteAgents)
    {
        // Deletes the user agent
        UserAgentInfoProvider.DeleteUserAgentInfo(agent);
    }
}

```

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

### Deleting a contact

```csharp

// Gets the first contact on the current site whose last name is 'Smith'
ContactInfo deleteContact = ContactInfoProvider.GetContacts()
                                    .WhereEquals("ContactLastName", "Smith")
                                    .WhereEquals("ContactSiteID", SiteContext.CurrentSiteID)
                                    .FirstObject;

if (deleteContact != null)
{
    // Deletes the contact
    ContactInfoProvider.DeleteContactInfo(deleteContact);
}

```

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

## Contact statuses

### Creating a contact status

```csharp

// Creates a new contact status object for the current site
ContactStatusInfo newStatus = new ContactStatusInfo()
{
    ContactStatusDisplayName = "New contact status",
    ContactStatusName = "NewContactStatus",
    ContactStatusSiteID = SiteContext.CurrentSiteID
};

// Saves the contact status to the database
ContactStatusInfoProvider.SetContactStatusInfo(newStatus);

```

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

### Updating a contact status

```csharp

// Gets the contact status
ContactStatusInfo updateStatus = ContactStatusInfoProvider.GetContactStatusInfo("NewContactStatus", SiteContext.CurrentSiteName);

if (updateStatus != null)
{
    // Updates the contact status properties
    updateStatus.ContactStatusDisplayName = updateStatus.ContactStatusDisplayName.ToLowerCSafe();

    // Saves the modified contact status to the database
    ContactStatusInfoProvider.SetContactStatusInfo(updateStatus);
}

```

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

### Updating multiple contact statuses

```csharp

// Gets all contact statuses from the current site whose code name starts with 'New'
var statuses = ContactStatusInfoProvider.GetContactStatuses()
                                                .WhereEquals("ContactStatusSiteID", SiteContext.CurrentSiteID)
                                                .WhereStartsWith("ContactStatusName","New");

// Loops through individual contact statuses
foreach (ContactStatusInfo contactStatus in statuses)
{
    // Updates the contact status properties
    contactStatus.ContactStatusDisplayName = contactStatus.ContactStatusDisplayName.ToUpper();

    // Saves the modified contact status to the database
    ContactStatusInfoProvider.SetContactStatusInfo(contactStatus);
}

```

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

### Assigning a status to a contact

```csharp

// Gets the first contact on the current site whose last name is 'Smith'
ContactInfo contact = ContactInfoProvider.GetContacts()
                                    .WhereEquals("ContactLastName", "Smith")
                                    .WhereEquals("ContactSiteID", SiteContext.CurrentSiteID)
                                    .FirstObject;

// Gets the contact status
ContactStatusInfo contactStatus = ContactStatusInfoProvider.GetContactStatusInfo("NewContactStatus", SiteContext.CurrentSiteName);

if ((contact != null) && (contactStatus != null))
{
    // Checks that the contact doesn't already have the given status
    if (contact.ContactStatusID != contactStatus.ContactStatusID)
    {
        // Assigns the status to the contact
        contact.ContactStatusID = contactStatus.ContactStatusID;

        // Saves the updated contact to the database
        ContactInfoProvider.SetContactInfo(contact);
    }
}

```

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

### Removing statuses from contacts

```csharp

// Gets the contact status
ContactStatusInfo contactStatus = ContactStatusInfoProvider.GetContactStatusInfo("NewContactStatus", SiteContext.CurrentSiteName);

if (contactStatus != null)
{
    // Gets all contacts from the current site that have the specified status
    var contacts = ContactInfoProvider.GetContacts()
                                        .WhereEquals("ContactSiteID", SiteContext.CurrentSiteID)
                                        .WhereEquals("ContactStatusID", contactStatus.ContactStatusID);

    // Loops through the contacts
    foreach (ContactInfo contact in contacts)
    {
        // Sets the status to 'None' for each contact
        contact.ContactStatusID = 0;

        // Saves the updated contact to the database
        ContactInfoProvider.SetContactInfo(contact);
    }
}

```

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

### Deleting a contact status

```csharp

// Gets the contact status
ContactStatusInfo deleteStatus = ContactStatusInfoProvider.GetContactStatusInfo("NewContactStatus", SiteContext.CurrentSiteName);

if (deleteStatus != null)
{
    // Deletes the contact status
    ContactStatusInfoProvider.DeleteContactStatusInfo(deleteStatus);
}

```

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