Creating a message with a script

 

Script without PublicAPI.

For the script to work correctly you need the following namespaces:

EleWise.ELMA.Messages
EleWise.ELMA.Documents

Namespaces:

using EleWise.ELMA.Messages.Models;
using Elewise.ELMA.Model.Services;
Script text:

//create new message: 
var m =  InterfaceActivator.Create<ChannelMessage>();
//add the user stored in the context.User variable of the User type to the list of recipients 
m.Recipients.Add(context.User); 
m.Subject = "Subject"; 
m.MessageType = ChannelMessageType.Post; 
//specify message author
m.CreationAuthor = context.User; 
//specify message creation date: 
m.CreationDate = DateTime.Now; 
//message contents: 
m.FullMessage = "Message text"; 
//create message status (new or read) 
var s = InterfaceActivator.Create<RecipientMessageStatus>(); 
s.Message = m; s.Recipient = context.User;
//message status: new 
s.Status = MessageStatus.New; s.Save(); 
//add status to the message 
m.Statuses.Add(s);             
//save message 
m.Save();​


 
Creating a similar script with PublicAPI
Updated documentation on PublicAPI is available here.

Context variabes:

context.Recipient - User type variable
context.User - User type variable

Namespace:

using EleWise.ELMA.API;

 

Script text:
PublicAPI.Portal.Messages.SendMessage("Message subject","Message text", context.Recipient, context.User); // Subject, Text, Recipient, Sender 
Starting from version 3.8 you can user Builder to create messages

Context variabes:

context.Recipient - User type variable
context.User - User type variable
 
Script text:
PublicAPI.Portal.Messages.MessageBuilder("Subject").MessageText("Text").Recipients(new[] { context.Recipient}).MessageAuthor(context.User).SendMessage();