Adding a version to a document using a script

This article provides a script for adding a new version (File context variable of the File type) to a document (Document context variable, whose type is a specific document type).

Script without using PublicAPI
For the script to work, add assemblies:
Elewise.ELMA.Documents
Elewise.ELMA.Documents.Docflow
 
Namespaces:
using EleWise.ELMA.Documents.Managers;
using EleWise.ELMA.Documents.Models;
using EleWise.ELMA.Services; 
using EleWise.ELMA.Model.Services;
 
Script text:
var version = InterfaceActivator.Create<DocumentVersion>();
version.Document = context.Document;
version.File=context.File;
version.Status = DocumentVersionStatus.Current;//set the Current version status
context.Document.Versions.Add(version);
context.Document.Save();
 
Below is an example of a script for generating a version file by a template, stored on the server hard drive.
 
For the script to work, add the assemblies: 
Elewise.ELMA.Documents
Elewise.ELMA.Documents.Docflow
 
Namespaces:
using EleWise.ELMA.Documents;
using EleWise.ELMA.Documents.Docflow;
using EleWise.ELMA.Documents.Models;
using EleWise.ELMA.Files;
using EleWise.ELMA.Model.Services;
using EleWise.ELMA.Runtime.Managers;
using EleWise.ELMA.Services;
using EleWise.ELMA.Templates;​

 
Context variables:
context.Doc - variable of the document type, to which the version file must be added
context.File - variable of the File type
 
Script text:
context.File = InterfaceActivator.Create<BinaryFile>();
context.File.Name="Name.docx"; //file version name (specify the extension)
context.File.CreateDate = DateTime.Now;
context.File.InitializeContentFilePath();
System.IO.File.Copy(@"c:\\Template1.docx", context.File.ContentFilePath); //the first function parameter - is the path to the file on the drive
var generator = Locator.GetServiceNotNull<DocumentGeneratorManager>().Create(context.File.ContentFilePath); //create a generator
generator.Generate(context);
Locator.GetServiceNotNull<IFileManager>().SaveFile(context.File); //save the file to the context
var version = InterfaceActivator.Create<DocumentVersion>(); //create a document version
version.Document = context.Doc;
version.File=context.File; //load the generated file to the version
version.Status = DocumentVersionStatus.Current; //set the Current status to the version
context.Doc.Versions.Add(version);
context.Doc.Save(); //save the document​

Script with PublicAPI

Note
Documentation on PublicAPI is available here
Attention
The script below is relevant for ELMA 3.12.1 or lower.
Context variables:
context.Document - variable of the Document type
context.File - variable of the File type
DocumentVersionStatus - optional parameter; if it is not specified, the Draft status is set

Add namespace:
using EleWise.ELMA.API;​

 
Script text:
PublicAPI.Docflow.DocumentVersion.AddDocumentVersion(context.Document, context.File, PublicAPI.Enums.Documents.DocumentVersionStatus.Current);  //Document, file, version status​

 

Below is an example of a script for generating a version file by a template, stored on the server hard drive.

Context variables:
context.Document - variable of the document type
context.File - variable of the file type
 
Namespace:
using EleWise.ELMA.Files;​

 
context.File = InterfaceActivator.Create<BinaryFile>();
context.File.Name = "Name.docx"; //version file name (specify the extension)
context.File.CreateDate = DateTime.Now;
context.File.InitializeContentFilePath();
System.IO.File.Copy(@"c:\\BASA\Document.docx", context.File.ContentFilePath); //the first function parameter is the path to the file on the hard drive
PublicAPI.Services.DocumentGenerator.Generate(context.File, context);
PublicAPI.Services.File.SaveFile(context.File); //save the file to the context
PublicAPI.Docflow.DocumentVersion.AddDocumentVersion(context.Document, context.File, PublicAPI.Enums.Documents.DocumentVersionStatus.Current); //Add the version to the document with the Current status