Posted on 25/06/2019 14:23:49
Hi Morten,
Thank you for the list!
I managed to solve the problem in a somehow messy way. I know that the groups that I want to sort by city will have a group name like 'Administration - Aarhus', so knowing from your reply that I cannot import information about the city easily, I chose to create an IndexBuilderExtender to separate the two values. The code is shown below. The name of the sortable groups are in this case always '{Subject area} - {City}', so a user can belong to several departments and several cities. To filter out as many groups as possible, I check whether the string ' - ' is present in the group name and if so, I split the group name and save them as two new fields, AccessUserSubjectAreas and AccessUserCities, in the document that is being indexed.
I can then use these two values in queries to filter the users correctly employee lists.
Best regards,
Roald, Novicell
public class UserIndexExtender : IIndexBuilderExtender<UserIndexBuilder>
{
public void ExtendDocument(IndexDocument indexDocument)
{
if (indexDocument.ContainsKey("GroupNames"))
{
var groups = ((IEnumerable)indexDocument["GroupNames"]).Cast<object>().Select(x => x.ToString()).ToArray();
List<string> subjectAreas = new List<string>();
List<string> cities = new List<string>();
foreach (string name in groups)
{
int dividerPos = name.IndexOf(" - ");
if (dividerPos > -1)
{
string subjectArea = name.Substring(0, dividerPos);
string city = name.Substring(dividerPos + 3, name.Length - dividerPos - 3);
subjectAreas.Add(subjectArea);
cities.Add(city);
}
}
if (subjectAreas.Any())
{
indexDocument.AddToIndex("AccessUserSubjectAreas", subjectAreas.ToArray());
}
if (cities.Any())
{
indexDocument.AddToIndex("AccessUserCities", cities.ToArray());
}
}
}
}
public static class IndexBuilderExtension
{
public static void AddToIndex(this IndexDocument document, string fieldName, object fieldValue)
{
if (!document.ContainsKey(fieldName))
{
document.Add(fieldName, fieldValue);
}
else
{
document[fieldName] = fieldValue;
}
}
}