Determining a user's department in a scipt

You can determine the department of a user with the following script, where the user is specified in the context.Poljzovatelj context variable.

Script without PublicAPI.

For correct functioning of the script you need the following namespace:
 
using EleWise.ELMA.Security.Models;

Script text:

var department = context.Poljzovatelj.OrganizationItems.ToArray() 
.Union(context.Poljzovatelj.OrganizationGroups) 
.Select(organizationItem => 
{ 
  var parentOrganizationItem = organizationItem.ParentItem;                                 
  while (parentOrganizationItem != null && parentOrganizationItem.ItemType == OrganizationItemType.Department)
  parentOrganizationItem = parentOrganizationItem.ParentItem;                                                               
  return parentOrganizationItem.ParentItem != null ? parentOrganizationItem.ParentItem : null; 
  }) 
  .Where(u => u != null).FirstOrDefault();
context.NaimenovanieOtdela = department.Name;
 
Creating a similar script with PublicAPI
Updated documentation on PublicAPI is available here.

Context variables:

context.Poljzovatelj - User variable of the User type
context.NaimenovanieOtdela - Department Name variable of the String type

Namespace:

using EleWise.ELMA.API;

Script text:

context.NaimenovanieOtdela = PublicAPI.Portal.Security.User.GetUserDepartments(context.Poljzovatelj).FirstOrDefault().Name;