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);


> Back to list of examples

Updating a department




// Gets the department
DepartmentInfo updateDepartment = DepartmentInfoProvider.GetDepartmentInfo("NewDepartment", SiteContext.CurrentSiteName);
if (updateDepartment != null)
{
    // Updates the department properties
    updateDepartment.DepartmentDisplayName = updateDepartment.DepartmentDisplayName.ToLower();

    // Saves the changes to the database
    DepartmentInfoProvider.SetDepartmentInfo(updateDepartment);
}


> Back to list of examples

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.ToUpper();

    // Saves the changes to the database
    DepartmentInfoProvider.SetDepartmentInfo(modifyDepartment);
}


> Back to list of examples

Adding users as managers of a department




// Gets the department
DepartmentInfo department = DepartmentInfoProvider.GetDepartmentInfo("NewDepartment", SiteContext.CurrentSiteName);
if (department != null)
{
    // Adds the current user as a manager of the department
    UserDepartmentInfoProvider.AddUserToDepartment(department.DepartmentID, MembershipContext.AuthenticatedUser.UserID);
}


> Back to list of examples

Removing users from a department




// Gets the department
DepartmentInfo department = DepartmentInfoProvider.GetDepartmentInfo("NewDepartment", SiteContext.CurrentSiteName);
if (department != null)
{
    // Gets an object representing the relationship between the department and the current user
    UserDepartmentInfo deleteUserDepartment = UserDepartmentInfoProvider.GetUserDepartmentInfo(department.DepartmentID, MembershipContext.AuthenticatedUser.UserID);

    if (deleteUserDepartment != null)
    {
        // Removes the current user from the department
        UserDepartmentInfoProvider.DeleteUserDepartmentInfo(deleteUserDepartment);
    }
}


> Back to list of examples

Deleting a department




// Gets the department
DepartmentInfo deleteDepartment = DepartmentInfoProvider.GetDepartmentInfo("NewDepartment", SiteContext.CurrentSiteName);

if (deleteDepartment != null)
{
    // Deletes the department
    DepartmentInfoProvider.DeleteDepartmentInfo(deleteDepartment);
}


> Back to list of examples

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);
}


> Back to list of examples

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);
    }
}


> Back to list of examples