Creating a message with a script

This article describes how you can create a message for a user with a script.

Script with PublicAPI

Note
Documentation on PublicAPI is available here
Attention
The script below is relevant for ELMA up to 3.12.1 inclusive.

Context variables:

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 
Note
Starting with ELMA 3.8 you can use 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();
 

Script without PublicAPI

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

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();​