Sending a document for approval using a script

This article provides an example of a script for sending a document for approval.

Script without PublicAPI

Context variables:

Document - variable of the Document (can be any document type) type. Stores the document which must be approved
Approvers - variable of the User type (Many-to-many link type). Stores the list of approvers

Namespaces:

using EleWise.ELMA.Documents.Managers;
using EleWise.ELMA.Documents.Models.Tasks;
using EleWise.ELMA.Security;
using EleWise.ELMA.Security.Models;
using EleWise.ELMA.Services;
using EleWise.ELMA.Tasks.Models;

Script text:

var iniciator = (User)EleWise.ELMA.Security.Services.AuthenticationService.GetCurrentUser<EleWise.ELMA.Security.Models.IUser>(); // - the authenticated user initiates the approval
var harm = new ApprovementTaskGroup(); // - create a new approval task group 
harm.CreationAuthor = iniciator; // - identify the approval author 
harm.CreationDate = DateTime.Now; // - approval date 
harm.Subject = "Approve "+ context.Document.Name.ToString(); // - approval group name
harm.ExecutionDate = harm.ExecutionDate.AddDays(1); // - approval tasks due dates
harm.Status = ApprovalStatus.None; // - set status - rejected
harm.ApprovementList = new ApprovementList(); // - create a new approval sheet
harm.ExecutionType = ApprovementExecutionType.Parallel; // - identify the approval type
harm.Items.Add(new DocumentTaskItem{ Document = context.Document, Version = context.Document.Versions.FirstOrDefault() }); // - select a document and its version for approval 
harm.Save(); // - save the approval group 
var securityService = Locator.GetServiceNotNull<ISecurityService>();
//next, usres are loaded to the list of the document approvers, taking into account possible substitutions.
//if you don't need to take substitutions into account, the mechanism can be made simpler
var users = new List<Pair<EleWise.ELMA.Security.Models.IUser, EleWise.ELMA.Security.Models.IUser>>(); 
context.Approvers.ToList().ForEach(user => { var addUser = user.ReplacementMode == ReplacementMode.AutoReplacement && user.ReplacementUser != null ?
                                                                        new Pair<EleWise.ELMA.Security.Models.IUser, EleWise.ELMA.Security.Models.IUser>(user.ReplacementUser, user) :
                                                                        new Pair<EleWise.ELMA.Security.Models.IUser, EleWise.ELMA.Security.Models.IUser>(user, null); 
                                                                        if (!users.Contains(addUser)) users.Add(addUser); }); 
securityService.RunByUser(iniciator, () => ApprovementTaskGroupManager.Instance.AddExecuters(harm, users)); // - add users to the list of approvers<span style="color: #333333; font-family: Tahoma; font-size: 13px;"> </span>
 
 
Creating a similar script with PublicAPI
Documentation on PublicAPI is available here

Context variables:

context.Document - variable of the Document type (Single, Many-to-many)
context.User - variable of the User type (Many-to-many), list of approvers
context.Date - variable of the Date type (stores the due date), optional parameter. The Can be empty box should be unchecked for this variable.

Namespace:
using EleWise.ELMA.API;​

Script text:
PublicAPI.Docflow.Tasks.SendToApprovement(context.Document, context.User, context.Date);    //Document, Users, Due date (optional parameter)​