Kentico CMS 7.0 Developer's Guide

Managing messages

Managing messages

Previous topic Next topic Mail us feedback on this topic!  

Managing messages

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example creates a message.

 

private bool CreateMessage()

{

  // Create new message object

  MessageInfo newMessage = new MessageInfo();

 

  // Set the properties

   newMessage.MessageSubject = "API example message";

   newMessage.MessageBody = "Hello! This is a sample message created by Kentico CMS API.";

 

  // Get sender and recipient of the message

  UserInfo sender = CMSContext.CurrentUser;

  UserInfo recipient = UserInfoProvider.GetUserInfo("administrator");

 

  // 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;

   }

 

  return false;

}

 

The following example gets and updates the message created by the code example above.

 

private bool GetAndUpdateMessage()

{

  // Prepare the parameters

  string where = "[MessageSubject] = 'API example message'";

 

  // Get the data

  DataSet messages = MessageInfoProvider.GetMessages(where, null, 1, null);

  if (!DataHelper.DataSourceIsEmpty(messages))

   {

      // Get the message from the DataSet

      MessageInfo modifyMessage = new MessageInfo(messages.Tables[0].Rows[0]);

 

      // Update the properties

       modifyMessage.MessageBody = modifyMessage.MessageBody.ToUpper();

 

      // Save the changes

      MessageInfoProvider.SetMessageInfo(modifyMessage);

 

      return true;

   }

 

  return false;

}

 

The following example gets and bulk updates multiple messages selected from database based on a WHERE condition.

 

private bool GetAndBulkUpdateMessages()

{

  // Prepare the parameters

  string where = "[MessageSubject] = 'API example message'";

 

  // Get the data

  DataSet messages = MessageInfoProvider.GetMessages(where, null);

  if (!DataHelper.DataSourceIsEmpty(messages))

   {

      // Loop through the individual items

      foreach (DataRow messageDr in messages.Tables[0].Rows)

       {

          // Create object from DataRow

          MessageInfo modifyMessage = new MessageInfo(messageDr);

 

          // Update the properties

           modifyMessage.MessageBody = modifyMessage.MessageBody.ToLower();

 

          // Save the changes

          MessageInfoProvider.SetMessageInfo(modifyMessage);

       }

 

      return true;

   }

 

  return false;

}

 

The following example deletes all messages created by the first code example on this page.

 

private bool DeleteMessage()

{

  // Prepare the parameters

  string where = "[MessageSubject] = 'API example message'";

 

  // Get the message

  DataSet messages = MessageInfoProvider.GetMessages(where, null);

 

  if (!DataHelper.DataSourceIsEmpty(messages))

   {

      foreach (DataRow messageDr in messages.Tables[0].Rows)

       {

          // Create message object from DataRow

          MessageInfo deleteMessage = new MessageInfo(messageDr);

 

          // Delete the message

          MessageInfoProvider.DeleteMessageInfo(deleteMessage);

       }

 

      return true;

   }

 

  return false;

}