---
title: Contact groups
---

> 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:

### Creating a contact group

```csharp

// Creates a new contact group object
ContactGroupInfo newGroup = new ContactGroupInfo()
{
    ContactGroupDisplayName = "New group",
    ContactGroupName = "NewGroup",
    ContactGroupEnabled = true,
    ContactGroupDynamicCondition = "{%Contact.ContactLastName.Contains(\"Smith\")%}"
};

// Saves the contact group to the database
ContactGroupInfo.Provider.Set(newGroup);

```

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

### Updating a contact group

```csharp

// Gets the contact group
ContactGroupInfo updateGroup = ContactGroupInfo.Provider.Get("NewGroup");

if (updateGroup != null)
{
    // Updates the contact group's properties
    updateGroup.ContactGroupDisplayName = updateGroup.ContactGroupDisplayName.ToLowerCSafe();

    // Saves the modified contact group to the database
    ContactGroupInfo.Provider.Set(updateGroup);
}

```

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

### Updating multiple contact groups

```csharp

// Gets all contact groups whose name starts with 'New'
var groups = ContactGroupInfo.Provider.Get().WhereStartsWith("ContactGroupName", "New");

// Loops through individual contact groups
foreach (ContactGroupInfo group in groups)
{
    // Updates the contact group properties
    group.ContactGroupDisplayName = group.ContactGroupDisplayName.ToUpper();

    // Saves the modified contact group to the database
    ContactGroupInfo.Provider.Set(group);
}

```

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

### Adding contacts to a contact group

```csharp

// Gets the contact group
ContactGroupInfo group = ContactGroupInfo.Provider.Get("NewGroup");

if (group != null)
{
    // Gets all contacts whose last name is 'Smith'
    var contacts = ContactInfo.Provider.Get().WhereEquals("ContactLastName", "Smith");

    // Loops through individual contacts
    foreach (ContactInfo contact in contacts)
    {
        // Creates an object representing the relationship between the contact and the group
        ContactGroupMemberInfo newContactGroupMember = new ContactGroupMemberInfo()
        {
            ContactGroupMemberContactGroupID = group.ContactGroupID,
            ContactGroupMemberType = ContactGroupMemberTypeEnum.Contact,
            ContactGroupMemberRelatedID = contact.ContactID,
            ContactGroupMemberFromManual = true
        };

        // Saves the contact-group relationship to the database
        ContactGroupMemberInfo.Provider.Set(newContactGroupMember);
    }
}

```

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

### Removing contacts from a contact group

```csharp

// Gets the contact group
ContactGroupInfo group = ContactGroupInfo.Provider.Get("NewGroup");

if (group != null)
{
    // Gets the first contact whose last name is 'Smith'
    ContactInfo contact = ContactInfo.Provider.Get()
                                        .WhereEquals("ContactLastName", "Smith")
                                        .TopN(1)
                                        .FirstOrDefault();

    // Gets the relationship between the contact and the contact group
    ContactGroupMemberInfo contactGroupMember =
        ContactGroupMemberInfo.Provider.Get(group.ContactGroupID, contact.ContactID, ContactGroupMemberTypeEnum.Contact);

    if (contactGroupMember != null)
    {
        // Removes the contact from the contact group
        ContactGroupMemberInfo.Provider.Delete(contactGroupMember);
    }
}

```

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

### Adding accounts to a contact group

```csharp

// Gets the contact group
ContactGroupInfo group = ContactGroupInfo.Provider.Get("NewGroup");

if (group != null)
{
    // Gets the first the account whose name starts with 'New"
    AccountInfo account = AccountInfo.Provider.Get()
                                        .WhereStartsWith("AccountName", "New")
                                        .TopN(1)
                                        .FirstOrDefault();

    // Creates an object representing the relationship between the account and the contact group
    ContactGroupMemberInfo newContactGroupAccountMember = new ContactGroupMemberInfo()
    {
        ContactGroupMemberContactGroupID = group.ContactGroupID,
        ContactGroupMemberType = ContactGroupMemberTypeEnum.Account,
        ContactGroupMemberRelatedID = account.AccountID
    };

    // Saves the account-contact group relationship to the database
    ContactGroupMemberInfo.Provider.Set(newContactGroupAccountMember);
}

```

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

### Removing accounts from a contact group

```csharp

// Gets the contact group
ContactGroupInfo group = ContactGroupInfo.Provider.Get("NewGroup");

if (group != null)
{
    // Gets the first the account whose name starts with 'New"
    AccountInfo account = AccountInfo.Provider.Get()
                                        .WhereStartsWith("AccountName", "New")
                                        .TopN(1)
                                        .FirstOrDefault();

    // Gets the relationship between the account and the contact group
    ContactGroupMemberInfo contactGroupAccountMember =
        ContactGroupMemberInfo.Provider.Get(group.ContactGroupID, account.AccountID, ContactGroupMemberTypeEnum.Account);

    if (contactGroupAccountMember != null)
    {
        // Removes the account from the contact group
        ContactGroupMemberInfo.Provider.Delete(contactGroupAccountMember);
    }
}

```

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

### Deleting a contact group

```csharp

// Gets the contact group
ContactGroupInfo deleteGroup = ContactGroupInfo.Provider.Get("NewGroup");

if (deleteGroup != null)
{
    // Deletes the contact group
    ContactGroupInfo.Provider.Delete(deleteGroup);
}

```

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