Registering a document in a script

A script for registering an existing document.
Before using the script, go to Administration - Document Management - Document Registration Variants and create a registration variant, specifying its name, the type of the registered document, document flow, registration office, and category. This registration variant will be used in the script.

Script without using PublicAPI

Context variables:
context.Doc - stores the document to register.

For the script to work correctly, add assemblies:

EleWise.ELMA.Documents;
EleWise.ELMA.Documents.Docflow;

Namespaces:

using EleWise.ELMA.Documents.Managers;
using EleWise.ELMA.Documents.Models;
using EleWise.ELMA.Documents.Models.Nomenclature;
using EleWise.ELMA.Extensions;
using EleWise.ELMA.Model.Common;
using EleWise.ELMA.Model.Entities;
using EleWise.ELMA.Model.Entities.EntityReferences;
using EleWise.ELMA.Model.Entities.ProcessContext;
using EleWise.ELMA.Model.Managers;
using EleWise.ELMA.Model.Metadata;
using EleWise.ELMA.Model.Services;
using EleWise.ELMA.Model.Types.Settings;
using EleWise.ELMA.Services;
using EleWise.ELMA.Documents.Contracts.Models;
 
Script text:
//identify the entity type based on the document type. 
//instead of DocType specify your document class name. Pay attention that the structure uses its interface
var docType =  new ReferenceOnEntityType { TypeUid = InterfaceActivator.UID<IDocType>(false) };
//find the registration variant
var reg_variant = RegistrationVariantManager.Instance
    .Find(new InstanceOf<IRegistrationVariantFilter>(){New =
    {
        //specify the document flow - Incoming
        Docflow = DocumentFlowType.Incoming, 
        //set the document type
        DocumentType = docType,
        //specify the UID for DepositoryGroup. Identifying the value is described below
        DepositoryGroupUid = new Guid("64AEC71A-D9F3-488E-8E4F-1AD47E31D373"),
        //set the registration variant
        Query = "Name = ’Variant1’"
        }}.New, new FetchOptions(0, 1))
    .FirstOrDefault();
//if the registration variant is not found, quit the script
if (reg_variant == null) {
        Console.Write("Variant not found");
        return;
    }
//find category
var depository = (IDepository)DepositoryGroupManager.Instance.GetByGroupUid(reg_variant.DepositoryGroupUid, true)
    .CastAsRealType();
//if the category is not found, quit the script
if (depository == null)
    {
        Console.Write("Depository not found");
        return;
    }
//search the record card type in the published records classification scheme
var filter = InterfaceActivator.Create<INomenclatureCacheFilter>();
filter.Depository = depository;
filter.DocumentType = docType;
filter.Flow = reg_variant.Docflow;
filter.RegistrationPlace = reg_variant.RegistrationPlace;           
//if the record card type is not found in the published records classification scheme, quit the script
var cacheItem = NomenclatureCacheManager.Instance.Find(filter, FetchOptions.All).FirstOrDefault();
if (cacheItem == null)
    {
        Console.Write("Cache item not found");
        return;
    }
//create a record card of the found type
var rkk = (IRegistrationCard)InterfaceActivator.Create(cacheItem.RegistrationCard.EntityType);
//write the registered document to it
rkk.Document = context.Doc;
//specify the category where the document is registered
rkk.Depository = depository;
//specify the document flow where the document is registered (in this case, Incoming)
rkk.Flow = reg_variant.Docflow;
//specify the registration office
rkk.RegistrationPlace = reg_variant.RegistrationPlace;
//and specify the type of the registered document in the record card:
rkk.DocumentType = docType;          
context.Doc.RegistrationCard.Add((RegistrationCard)rkk);
rkk.Save();
//Registration itself. A number is generated inside
RegistrationCardManager.Instance.Register(rkk, false, null);

 
Creating a similar script with PublicAPI
Documentation on PublicAPI is available here
Context variables:

context.Document - variable of the Document type. 
context.RegistrationVariant - variable of the Registration Variant type
context.Category - variable of the Category type

Namespace:

using EleWise.ELMA.API;
 

Script text:

PublicAPI.Docflow.Document.Register(context.Document, context.RegistrationVariant);
PublicAPI.Docflow.Document.Register(context.Document, context.Category, PublicAPI.Enums.Documents.DocumentFlowType.Incoming); 
//DocumentFlowType.Incoming - incoming document flow
 

You can identify the UID for the DepositoryGroup using this query to the database:

select [GroupUid] from [DepositoryGroup] where [Name] = ’Category_name’

This query can be executed in the DBMS (e.g. in MS SQL Server Management Studio for MS SQL or in IBExpert for Firebird). 
If you cannot use a DBMS, you can see the result of the query in ELMA. For this, you need to create a new report and use the query in it. Place the GroupUid field to the report layout (in FastReport) and start debugging. The report web page will display the Uid.
An alternative way of identifying the GroupUid:
- open the page of the required category in Administration - Document Management - Records Classification Scheme, where the document should be registered and find its id (a field on the page).
- in a separate script, use this line:
Console.WriteLine(DepositoryManager.Instance.Load(4).GroupUid.ToString());
Instead of 4 specify the category id.
Launch the debugging of the script: the console on the debugging page will display the required Uid, copy it to the document registration script.