Specifying a user's superior in a script

This article explains how to determine a user's direct superior in a script. 

Script without PublicAPI.

The script uses the following context variables:

context.Ispolnitelj - "Executor" variable of the User type
context.Nachaljnik - "Superior" variable of the User type

Script text:

var chiefs = context.Ispolnitelj.OrganizationItems.ToArray ().Union (context.Ispolnitelj.OrganizationGroups).Select (organizationItem =>  {
    var parentOrganizationItem = organizationItem.ParentItem;
    while (parentOrganizationItem != null && parentOrganizationItem.User == null)
        parentOrganizationItem = parentOrganizationItem.ParentItem;
    return parentOrganizationItem != null ? parentOrganizationItem.User : null;
}).Where (u => u != null);
 
if(!chiefs.Any()) //If no element of the organizational structure is found (top level)
    context.Nachaljnik = context.Ispolnitelj; //The initiator is specified as their superior
 
else //Standard path
{
    var chief = chiefs.First ();
    context.Nachaljnik = chief;
}
If the user who starts the process is the highest element of the organizational structure, he or she will be specified as their superior.
 
Creating a similar script with PublicAPI
Updated documentation on PublicAPI доступна по ссылке

The script writes all the superiors of a user to the context.Nachaljniki variable (not only the head of department, but all the users who are superior in the organizational structure).

Context variables:

context.Ispolnitelj - Executor, User type
context.Nachaljniki - Superiors, User type, link type - list

Namespace:

using EleWise.ELMA.API;

Script text:

foreach (var chief in PublicAPI.Portal.Security.OrganizationItem.GetChiefByUser(context.Ispolnitelj))
{
    context.Nachaljniki.Add(superior);
}