logo

Getting a user's superior in a script

This article explains how to determine a user's direct superior in 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.

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

Context variables:

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

Namespace:

using EleWise.ELMA.API;

Script text:

foreach (var superior in PublicAPI.Portal.Security.OrganizationItem.GetChiefByUser(context.Executor))
{
    context.Superiors.Add(superior);
}

Script without PublicAPI

The script uses the following context variables:

context.Executor - Executor, User type
context.Superior - Superior, User type

Script text:

var superiors = context.Executor.OrganizationItems.ToArray ().Union (context.Executor.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(!superiors.Any()) //if no organizational structure elements found (top level element)
    context.Superior = context.Executor; //the initiator is selected as the superior
 
else //standard selection
{
    var superior = superiors.First ();
    context.Superior = superior;
}
If the user who starts the process is the highest element of the organizational structure, he or she will be specified as the superior.