using System; using System.Data; using CMS.CMSHelper; using CMS.GlobalHelper; using CMS.LicenseProvider; using CMS.Messaging; using CMS.SettingsProvider; using CMS.SiteProvider; using CMS.UIControls; [Title(Text = "Messaging", ImageUrl = "CMSModules/CMS_Messaging/module.png")] public partial class CMSAPIExamples_Code_Community_Messaging_Default : CMSAPIExamplePage { #region "Initialization" protected void Page_Load(object sender, EventArgs e) { // Check license LicenseHelper.CheckFeatureAndRedirect(URLHelper.GetCurrentDomain(), FeatureEnum.Messaging); // Message apiCreateMessage.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(CreateMessage); apiGetAndUpdateMessage.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndUpdateMessage); apiGetAndBulkUpdateMessages.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndBulkUpdateMessages); apiDeleteMessage.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(DeleteMessage); // Contact list apiAddUserToContactList.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(AddUserToContactList); apiRemoveUserFromContactList.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(RemoveUserFromContactList); // Ignore list apiAddUserToIgnoreList.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(AddUserToIgnoreList); apiRemoveUserFromIgnoreList.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(RemoveUserFromIgnoreList); } #endregion #region "Mass actions" /// /// Runs all creating and managing examples. /// public override void RunAll() { base.RunAll(); // Message apiCreateMessage.Run(); apiGetAndUpdateMessage.Run(); apiGetAndBulkUpdateMessages.Run(); // Contact list apiAddUserToContactList.Run(); // Ignore list apiAddUserToIgnoreList.Run(); } /// /// Runs all cleanup examples. /// public override void CleanUpAll() { base.CleanUpAll(); // Message apiDeleteMessage.Run(); // Contact list apiRemoveUserFromContactList.Run(); // Ignore list apiRemoveUserFromIgnoreList.Run(); } #endregion #region "API examples - Message" /// /// Creates a message. Called when the "Create message" button is pressed. /// 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; } /// /// Gets and updates the message created by the previous method. Called when the "Get and update message" button is pressed. /// Expects the CreateMessage method to be run first. /// 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; } /// /// Gets and bulk updates messages created by multiple executions of the first method. Called when the "Get and bulk update messages" button is pressed. /// Expects the CreateMessage method to be run first and multiple times to demonstrate the full functionality. /// 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.ToLowerCSafe(); // Save the changes MessageInfoProvider.SetMessageInfo(modifyMessage); } return true; } return false; } /// /// Deletes message(s). Called when the "Delete message" button is pressed. /// Expects the CreateMessage method to be run first. /// 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; } #endregion #region "API examples - Contact list" /// /// Adds the sample user to the current user's contact list. Called when the "Add user to contact list" button is pressed. /// private bool AddUserToContactList() { // First create a new user which will be added to the contact list UserInfo user = new UserInfo(); user.UserName = "MyNewContact"; user.FullName = "My new contact"; user.UserGUID = Guid.NewGuid(); UserInfoProvider.SetUserInfo(user); if (!ContactListInfoProvider.IsInContactList(CMSContext.CurrentUser.UserID, user.UserID)) { // Adds "Andy" to the current user's contact list ContactListInfoProvider.AddToContactList(CMSContext.CurrentUser.UserID, user.UserID); return true; } return false; } /// /// Removes the sample "Andy" user from the current user's contact list. Called when the "Remove user from contact list" button is pressed. /// Expects the AddUserToContactList method to be run first. /// private bool RemoveUserFromContactList() { // Gets "Andy" UserInfo object UserInfo user = UserInfoProvider.GetUserInfo("MyNewContact"); if (ContactListInfoProvider.IsInContactList(CMSContext.CurrentUser.UserID, user.UserID)) { // Removes "Andy" from the current user's contact list ContactListInfoProvider.RemoveFromContactList(CMSContext.CurrentUser.UserID, user.UserID); user.Delete(); return true; } user.Delete(); return false; } #endregion #region "API examples - Ignore list" /// /// Adds the sample user to the current user's ignore list. Called when the "Add user to ignore list" button is pressed. /// private bool AddUserToIgnoreList() { // First create a new user which will be added to the ignore list UserInfo user = new UserInfo(); user.UserName = "MyNewIgnoredUser"; user.FullName = "My new ignored user"; user.UserGUID = Guid.NewGuid(); UserInfoProvider.SetUserInfo(user); if (!IgnoreListInfoProvider.IsInIgnoreList(CMSContext.CurrentUser.UserID, user.UserID)) { // Adds "Andy" to the current user's ignore list IgnoreListInfoProvider.AddToIgnoreList(CMSContext.CurrentUser.UserID, user.UserID); return true; } return false; } /// /// Removes the sample "Andy" user from the current user's ignore list. Called when the "Remove user from ignore list" button is pressed. /// Expects the AddUserToIgnoreList method to be run first. /// private bool RemoveUserFromIgnoreList() { // Gets "Andy" UserInfo object UserInfo user = UserInfoProvider.GetUserInfo("MyNewIgnoredUser"); if (IgnoreListInfoProvider.IsInIgnoreList(CMSContext.CurrentUser.UserID, user.UserID)) { // Removes "Andy" from the current user's ignore list IgnoreListInfoProvider.RemoveFromIgnoreList(CMSContext.CurrentUser.UserID, user.UserID); user.Delete(); return true; } user.Delete(); return false; } #endregion }