logo

Getting users by an organizational structure item with a script

Sometimes you may need to get the users assigned to a specific job position in a script. 
For this, use the method GetUsersByDepartment.

How to use the method:

  • find an organizational structure item by ID or name;
  • get the users on the selected job position.

There are two ways to find an organizational structure item: by ID and by name. 
You can find the ID of an organizational structure item in the database, in the OrganizationItem table.

To get the users who have a specific job position, use the method GetUsersByDepartament, having passed the organizational structure item as a parameter. This method always returns a collection of users. If only one user can be assigned on the selected job position, then you can get them from the collection with the method FirstOrDefault().

Scripts with PublicAPI

Note
Documentation on PublicAPI is available here
Attention
The script below is relevant for ELMA up to 3.12.1 inclusive.

Namespace:

using EleWise.ELMA.API;

Context variable:

context.User - variable of the User type

Script text:

//Get an organizational structure item by ID
context.User = PublicAPI.Portal.Security.OrganizationItem.GetUsersByDepartament(PublicAPI.Portal.Security.OrganizationItem.LoadOrNull(1)).FirstOrDefault();

Context variable:

context.Users - variable of the User type, List link type

Script:

//Get an organizational structure item by name and add it to the context.Users context variable
context.Users.AddAll(PublicAPI.Portal.Security.OrganizationItem.GetUsersByDepartament(PublicAPI.Portal.Security.OrganizationItem.Find("Name = ’Legal Department’").FirstOrDefault()));
Getting users included in a certain group is described here

Script without PublicAPI

Context variables:
context.User - variable of the User type, Single link type;
context.UserList - variable of the User type, List link type.

Add namespaces:

using EleWise.ELMA.Security.Managers;
using EleWise.ELMA.Security.Models;
using EleWise.ELMA.Model.Services;

Script text:

//get an organizational structure item by ID
var orgItemPost = OrganizationItemManager.Instance.LoadOrNull(1);
//check if an organizational structure item with this ID was found
if (orgItemPost != null) {
    //get the user, who has a specific job position
    var userOnPost = (User)UserManager.Instance.GetUsersByDepartament(orgItemPost).FirstOrDefault();
    //write the user to a context variable
    context.User = userOnPost;
} else {
    //actions if an organizational structure with this ID is not found
}
 
//get an organizational structure item by name
var orgitemFilter = InterfaceActivator.Create<Filter>();
orgitemFilter.Query = "Name LIKE ’Legal Department’";
 
var ortItemDepartment = OrganizationItemManager.Instance.Find(orgitemFilter, null).FirstOrDefault();
//check if an organizational structure item with this name was found
if (ortItemDepartment != null) {
    //get the users, who have a specific job position
    var userList = UserManager.Instance.GetUsersByDepartament(ortItemDepartment);
    //write the users to a context variable
    foreach (var user in userList) {
        context.UserList.Add((User)user);
    }
} else {
    //actions, if an organizational structure with this name is not found
}