Customizing Usernames Displayed in the Kentico Admin Interface
Companies typically develop large, company-wide applications to solve critical business needs. With so many people using the system for different needs, it can become increasingly important to identify a user within the site easily. While Kentico’s default format for displaying a user will work for many companies, it may be necessary to customize this behavior to ensure that site administrators can properly identify who performed what action within the site. In this article, I’ll show you how easy it is to customize this behavior within a Kentico site.
As more and more users are accessing and updating a Kentico site, the task of identifying and tracking users can become increasingly challenging. Usernames may be a subjective value, such as [FirstName]LastIntial] (JohnS) or other variations. When more than one “John S” joins the team, knowing who did what can quickly prove to be a difficult task. Luckily, there is a simple way to solve this issue within a Kentico site.
Solution
As in many areas of Kentico, customizing displayed users within the site is a simple process that only requires updating a single function.
In the standard AppCode/CMS/Functions.cs (or Old_AppCode/CMS/Functions.cs, if using a web application project) class, there is a function named GetFormattedUserName. This function is called in several areas of the admin site when a user needs to be displayed, such as the Event Log and user selection dialogs.
Out of the box, Kentico uses the built-in UserInfoProvider.GetFormattedUserName function to retrieve the value.
public static string GetFormattedUserName(string username, string fullname, string nickname, bool isLiveSite = false)
{
return UserInfoProvider.GetFormattedUserName(username, fullname, nickname, isLiveSite);
}
This results in the user displayed using only the username value. You can find out more about the macro that performs the format here.
To change this format, we just need to update the GetFormattedUserName function in the Functions.cs class to display the values we need.
In my example, I’m assuming I have a lot of users with a username format of [FirstName][LastIntial]. In this scenario, many usernames may be very similar or not descriptive enough for someone to identify the user properly. Being able to see someone’s email may be beneficial.
To accomplish this, I replaced the standard UserInfoProvider.GetFormattedUserName call with my own logic. In my code, I retrieve the email address for the user to display alongside the username.
public static string GetFormattedUserName(string username, string fullname, string nickname, bool isLiveSite = false)
{
//return UserInfoProvider.GetFormattedUserName(username, fullname, nickname, isLiveSite);
// Format the displayed username to remove the site
username = UserInfoProvider.TrimSitePrefix(username);
// Get the userinfo object
UserInfo ui = UserInfoProvider.GetUserInfo(username);
if (ui != null)
{
// Make sure the user has an email
if (!String.IsNullOrEmpty(DataHelper.GetNotEmpty(ui.Email, "").Trim()))
{
// Return the formatted username / email
return String.Format("{0} ({1})", username, ui.Email);
}
else
{
// Return the username
return username;
}
}
else
{
// Return the username
return username;
}
}
With this change in place, the usernames displayed now include the email address. This will help determine the user who performed the action.
You can find out more details about this customization in the documentation here.
Moving Forward
With this simple change, you can modify how values are displayed throughout the site. Developers use techniques such as these to tailor the admin experience to their users and business, ensuring that the application continues to be a useful and efficient tool within their business. Good luck!
This blog is intended for informational purposes only and provides an example of one of the many ways to accomplish the described task. Always consult Kentico Documentation for the best practices and additional examples that may be more effective in your specific situation.