Departments
List of examples:
Departments
Creating a new department
// Creates a new department object
DepartmentInfo newDepartment = new DepartmentInfo();
// Sets the department properties
newDepartment.DepartmentDisplayName = "New department";
newDepartment.DepartmentName = "NewDepartment";
newDepartment.DepartmentSiteID = SiteContext.CurrentSiteID;
// Saves the department to the database
DepartmentInfoProvider.SetDepartmentInfo(newDepartment);
Updating a department
// Gets the department
DepartmentInfo updateDepartment = DepartmentInfoProvider.GetDepartmentInfo("NewDepartment", SiteContext.CurrentSiteName);
if (updateDepartment != null)
{
// Updates the department properties
updateDepartment.DepartmentDisplayName = updateDepartment.DepartmentDisplayName.ToLowerCSafe();
// Saves the changes to the database
DepartmentInfoProvider.SetDepartmentInfo(updateDepartment);
}
Updating multiple departments
// Gets all departments whose code name starts with 'NewDepartment'
var departments = DepartmentInfoProvider.GetDepartments().WhereStartsWith("DepartmentName", "NewDepartment");
// Loops through the departments
foreach (DepartmentInfo modifyDepartment in departments)
{
// Updates the department properties
modifyDepartment.DepartmentDisplayName = modifyDepartment.DepartmentDisplayName.ToUpperCSafe();
// Saves the changes to the database
DepartmentInfoProvider.SetDepartmentInfo(modifyDepartment);
}
Deleting a department
// Gets the department
DepartmentInfo deleteDepartment = DepartmentInfoProvider.GetDepartmentInfo("NewDepartment", SiteContext.CurrentSiteName);
if (deleteDepartment != null)
{
// Deletes the department
DepartmentInfoProvider.DeleteDepartmentInfo(deleteDepartment);
}
Department tax classes
Applying tax classes to a department
// Gets the department
DepartmentInfo department = DepartmentInfoProvider.GetDepartmentInfo("NewDepartment", SiteContext.CurrentSiteName);
// Gets the tax class
TaxClassInfo taxClass = TaxClassInfoProvider.GetTaxClassInfo("NewClass", SiteContext.CurrentSiteName);
if ((department != null) && (taxClass != null))
{
// Adds the tax class to the department
DepartmentTaxClassInfoProvider.AddTaxClassToDepartment(taxClass.TaxClassID, department.DepartmentID);
}
Removing tax classes from a department
// Gets the department
DepartmentInfo department = DepartmentInfoProvider.GetDepartmentInfo("NewDepartment", SiteContext.CurrentSiteName);
// Gets the tax class
TaxClassInfo taxClass = TaxClassInfoProvider.GetTaxClassInfo("NewClass", SiteContext.CurrentSiteName);
if ((department != null) && (taxClass != null))
{
// Gets an object representing the relationship between the department and the tax class
DepartmentTaxClassInfo departmentTax = DepartmentTaxClassInfoProvider.GetDepartmentTaxClassInfo(department.DepartmentID, taxClass.TaxClassID);
if ((departmentTax != null))
{
// Removes the tax class from the department
DepartmentTaxClassInfoProvider.DeleteDepartmentTaxClassInfo(departmentTax);
}
}