---
title: Messages
---

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

## Messages

### Creating a message

```csharp

// Creates a new message object
MessageInfo newMessage = new MessageInfo();

// Sets the message content
newMessage.MessageSubject = "API example message";
newMessage.MessageBody = "This is a sample message created by the Kentico API.";

// Gets the sender and the recipient of the message
UserInfo sender = MembershipContext.AuthenticatedUser;
UserInfo recipient = UserInfoProvider.AdministratorUser;

// Checks if both sender and recipient exist and sets the message properties
if ((sender != null) && (recipient != null))
{
    newMessage.MessageSenderUserID = sender.UserID;
    newMessage.MessageSenderNickName = sender.UserNickName;
    newMessage.MessageRecipientUserID = recipient.UserID;
    newMessage.MessageRecipientNickName = recipient.UserNickName;
    newMessage.MessageSent = DateTime.Now;

    // Saves the message to the database
    MessageInfoProvider.SetMessageInfo(newMessage);
}

```

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

### Updating a message

```csharp

// Prepares a where condition for loading a message with the subject 'API example message'
string where = "[MessageSubject] = N'API example message'";

// Gets the message based on the condition
DataSet messages = MessageInfoProvider.GetMessages(where, null);

if (!DataHelper.DataSourceIsEmpty(messages))
{
    // Gets the message from the DataSet
    MessageInfo modifyMessage = new MessageInfo(messages.Tables[0].Rows[0]);

    // Updates the properties of the message
    modifyMessage.MessageBody = modifyMessage.MessageBody.ToUpper();

    // Saves the changes to the database
    MessageInfoProvider.SetMessageInfo(modifyMessage);
}

```

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

### Updating multiple messages

```csharp

// Prepares a where condition for loading all messages whose subject contains the word 'API'
string where = "[MessageSubject] LIKE N'API'";

// Gets the messages that fulfill the condition
DataSet messages = MessageInfoProvider.GetMessages(where, null);

if (!DataHelper.DataSourceIsEmpty(messages))
{
    // Loops through individual messages
    foreach (DataRow messageDr in messages.Tables[0].Rows)
    {
        // Converts the DataRow to a message object
        MessageInfo modifyMessage = new MessageInfo(messageDr);

        // Updates the properties of the message
        modifyMessage.MessageBody = modifyMessage.MessageBody.ToLowerCSafe();

        // Saves the changes to the database
        MessageInfoProvider.SetMessageInfo(modifyMessage);
    }
}

```

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

### Deleting messages

```csharp

// Prepares a where condition for loading all messages whose subject contains the word 'API'
string where = "[MessageSubject] LIKE N'API'";

// Gets the messages that fulfill the condition
DataSet messages = MessageInfoProvider.GetMessages(where, null);

if (!DataHelper.DataSourceIsEmpty(messages))
{
    // Loops through individual messages
    foreach (DataRow messageDr in messages.Tables[0].Rows)
    {
        // Converts the DataRow to a message object
        MessageInfo deleteMessage = new MessageInfo(messageDr);

        if (deleteMessage != null)
        {
            // Deletes the message
            MessageInfoProvider.DeleteMessageInfo(deleteMessage);
        }
    }
}

```

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

## Contact lists

### Adding users to a contact list

```csharp

// Creates a new sample user which will be added to the contact list
UserInfo user = new UserInfo();
user.UserName = "John";
user.FullName = "John Doe";

UserInfoProvider.SetUserInfo(user);

// Checks that new user is not already in the current user's contact list
if (!ContactListInfoProvider.IsInContactList(MembershipContext.AuthenticatedUser.UserID, user.UserID))
{
    // Adds the new user to the current user's contact list
    ContactListInfoProvider.AddToContactList(MembershipContext.AuthenticatedUser.UserID, user.UserID);
}

```

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

### Removing users from a contact list

```csharp

// Gets the user
UserInfo user = UserInfoProvider.GetUserInfo("John");

// Checks that the user is in the current user's contact list
if (ContactListInfoProvider.IsInContactList(MembershipContext.AuthenticatedUser.UserID, user.UserID))
{
    // Removes the user from the current user's contact list
    ContactListInfoProvider.RemoveFromContactList(MembershipContext.AuthenticatedUser.UserID, user.UserID);
}

```

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

## Ignore lists

### Adding users to an ignore list

```csharp

// Creates a new sample user which will be added to the ignore list
UserInfo user = new UserInfo();
user.UserName = "Jane";
user.FullName = "Jane Doe";

UserInfoProvider.SetUserInfo(user);

// Checks that new user is not already in the current user's ignore list
if (!IgnoreListInfoProvider.IsInIgnoreList(MembershipContext.AuthenticatedUser.UserID, user.UserID))
{
    // Adds the new user to the current user's ignore list
    IgnoreListInfoProvider.AddToIgnoreList(MembershipContext.AuthenticatedUser.UserID, user.UserID);
}

```

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

### Removing users from an ignore list

```csharp

// Gets the user
UserInfo user = UserInfoProvider.GetUserInfo("Jane");

// Checks that the user is in the current user's ignore list
if (IgnoreListInfoProvider.IsInIgnoreList(MembershipContext.AuthenticatedUser.UserID, user.UserID))
{
    // Removes the user from the current user's ignore list
    IgnoreListInfoProvider.RemoveFromIgnoreList(MembershipContext.AuthenticatedUser.UserID, user.UserID);
}

```

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