API
Version 7.x > API > How Send Private Message to multiple users at the same time? View modes: 
User avatar
Member
Member
iacido-gmail - 1/15/2014 11:27:58 AM
   
How Send Private Message to multiple users at the same time?
Hello Friends,

I'm trying to send a private message to multiple users using the API, however I can send it to one by using this code:
// Check if both sender and recipient exist
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;

// Save the message
MessageInfoProvider.SetMessageInfo(newMessage);

return true;
}

However, is there a way to send to multiple users at the same time without need to execute a For each method to send the message one by one?

Thanks in advance.

User avatar
Kentico Consulting
Kentico Consulting
Kentico_RichardS - 1/16/2014 2:34:40 AM
   
RE:How Send Private Message to multiple users at the same time?
Hi,

Thank you for your message.

You cannot set the recipient to contain more recipients and the for cyclus or foreach cyclus is what you need to do. It can look similar to this simple example I just tested:


// Users Array
int[] UserListID = new int[4] { 66, 68, 69, 70 };

// loop and send message
foreach (int i in UserListID)
{
CMS.Messaging.MessageInfo MyMessage = new CMS.Messaging.MessageInfo();
MyMessage.MessageSenderUserID = 53;
MyMessage.MessageSent = DateTime.Now;
MyMessage.MessageBody = "Hello, test message";
MyMessage.MessageRecipientUserID = i;
MyMessage.MessageSenderNickName = i.ToString();
MyMessage.MessageRecipientNickName = i.ToString();
CMS.Messaging.MessageInfoProvider.SetMessageInfo(MyMessage);
}


Let me know if you need further assistance.

Kind regards,
Richard Sustek