logo

Creating a user task with a script

This article describes how to create a task with an attached document using 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.User - variable of the User type
context.Document - variable of the Document type

Namespace:

using EleWise.ELMA.API;

Script:

var task = PublicAPI.Portal.TaskBase.Task.Create();
task.Subject = "Task subject";
task.Executor = context.User;
task.StartDate = DateTime.Now;
task.EndDate = DateTime.Now.AddDays(1);
task.Description = "Task description";
var attachment = new DocumentAttachment{
            Document=context.Document
            };
task.DocumentAttachments.Add(attachment);
task.Save();
 

Script without PublicAPI

Context variables:

context.Executor - variable of the User type
context.Document - variable of the File type

For the script to work, add assemblies:

Elewise.ELMA.Project
Elewise.ELMA.Documents
Elewise.ELMA.Documents.Docflow
Elewise.ELMA.Tasks

Namespaces:

using EleWise.ELMA.Common.Models;
using EleWise.ELMA.Documents.Managers;
using EleWise.ELMA.Documents.Models;
using EleWise.ELMA.Model.Managers;
using EleWise.ELMA.Projects.Models;
using EleWise.ELMA.Tasks.Managers;
using EleWise.ELMA.Tasks.Models;
using EleWise.ELMA.Model.Services;

Script:

var tsk= InterfaceActivator.Create<Task>();
tsk.Subject="Task subject";
tsk.Executor=context.Executor;
tsk.StartDate=DateTime.Now;// start date - current date
tsk.EndDate=DateTime.Now.AddDays(1); //end date - current date + one day
tsk.Description="Task description";
if(context.Document!=null)
{
    var attachment = new DocumentAttachment{
        Document=context.Document
    };
    tsk.DocumentAttachments.Add(attachment);
}
tsk.Save();