Querying departments
This topic contains the following:
- Querying a single department
- Querying departments by a parent department
- Querying all departments
Querying a single department
To query a single department, you must perform the following:
- Get an instance of the manager.
Get an instance of the TaxonomyManager object.
- Get the Departments taxonomy.
To get the Departments taxonomy, call the GetTaxonomies method and filter the collection by the title of the taxonomy.
- Get the specified department.
To get the specified department, you must filter the Taxa collection of the taxonomy by the desired criteria.
Here are examples of querying a single department by ID and by title:
Querying department by name
public static Taxon GetDepartmentByName(string name)
{
TaxonomyManager taxonomyManager = TaxonomyManager.GetManager();
HierarchicalTaxonomy departments = taxonomyManager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name == "Departments").Single();
if (departments == null)
{
return null;
}
return departments.Taxa.Where(t => t.Name == name).SingleOrDefault();
}
Querying department by title
public static Taxon GetDepartmentByTitle(string title)
{
TaxonomyManager taxonomyManager = TaxonomyManager.GetManager();
HierarchicalTaxonomy departments = taxonomyManager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name == "Departments").Single();
if (departments == null)
{
return null;
}
return departments.Taxa.Where(t => t.Title == title).SingleOrDefault();
}
NOTE: Filtering the Taxa collection by title is case-sensitive.
Querying department by ID
public static Taxon GetDepartmentById(Guid id)
{
TaxonomyManager taxonomyManager = TaxonomyManager.GetManager();
HierarchicalTaxonomy departments = taxonomyManager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name == "Departments").Single();
if (departments == null)
{
return null;
}
return departments.Taxa.Where(t => t.Id == id).SingleOrDefault();
}
Querying departments by parent
To query all departments under a specified parent department, you must perform the following:
- Get an instance of the manager.
Get an instance of the TaxonomyManager object.
- Get the Departments taxonomy.
To get the Departments taxonomy, call the GetTaxonomies method and filter the collection by the title of the taxonomy.
- Get the departments.
To get the specified departments, you must filter the Taxa collection of the taxonomy by the name of the parent department.
Here is a code example:
public static IList<Taxon> GetDepartmentsByParent(string parentDepartment)
{
TaxonomyManager taxonomyManager = TaxonomyManager.GetManager();
HierarchicalTaxonomy departments = taxonomyManager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name == "Departments").Single();
if (departments == null)
{
return null;
}
return departments.Taxa.Where(t => t.Parent != null && t.Parent.Title == parentDepartment).ToList();
}
NOTE: Because the Parent property of the first level of departments is null, you must make a check whether the Parent property is null.
Querying all departments
To query all departments, you must perform the following:
- Get an instance of the manager.
Get an instance of the TaxonomyManager object.
- Get the Departments taxonomy.
To get the Departments taxonomy, call the GetTaxonomies method and filter the collection by the title of the taxonomy.
- Get the departments.
To get the departments, you must return the Taxa collection of the Departments taxonomy.
Here is a code example:
public static IList<Taxon> GetDepartments()
{
TaxonomyManager taxonomyManager = TaxonomyManager.GetManager();
HierarchicalTaxonomy departments = taxonomyManager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name == "Departments").Single();
if (departments == null)
{
return null;
}
return departments.Taxa;
}