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.User context variable.
Script with PublicAPI
Note
Documentation on PublicAPI is available here
Attention
The script below is relevant for ELMA up to 3.12.1 inclusive.
Context variables:
context.User - User variable of the User type
context.DepartmentName - Department Name variable of the String type
Namespace:
using EleWise.ELMA.API;
Script text:
context.DepartmentName = PublicAPI.Portal.Security.User.GetUserDepartments(context.User).FirstOrDefault().Name;
Script without PublicAPI
For correct functioning of the script you need the following namespace:
using EleWise.ELMA.Security.Models;
Script text:
var department = context.User.OrganizationItems.ToArray()
.Union(context.User.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.DepartmentName = department.Name;