Change UserName to First Name and Last Name in Forums
I need to change the "List of Forums", "List of Threads" and "Posts of One Thread" templates to display the users actual first and last names rather than user names. How can this be done?
I know this is probably not normally recommended, however this is an extranet type of application so the users all know each other by name.
Hi,
By default the Nickname field gets populated automatically from the UserName field value, unless you have specified otherwise.
To elaborate a bit further on the above statement, please check the following information:
By default we use the following templateable control for displaying the data:
<
sf:Conditional
If='<%# Eval("UserProfileUrl") == null %>' runat="server">
<Then>
<%# Eval("UserNickname") %>
</Then>
<Else>
<a href='<%# Eval("UserProfileUrl") %>' class="sfforumUser">
<span>
<%# Eval("UserNickname") %>
</span>
</a>
</Else>
</sf:Conditional>
where UserNickname
would be resolved from the user's profile:
UserNickname = processor.GetUserNickname(thread.Owner)
which gets the user profile for the passed owner
if the passed id is null or empty we return Nickname "Anonymous"
if the passed id is correct we retrieve the UserProfile object for this user, and retrieve the Nickname directly from the profile
if the passed id is correct but there is no profile for this user we return Nickname "Missing user"
Hope you find the above information useful
Regards,
Boyan Barnev
the Telerik team
Thanks for the response Boyan, I'm new to Sitefinity and still trying to figure out how it functions. I'm afraid I'm having difficulty seeing how what you've posted will help me replace the users Nickname with their actualy first and last name. Could you elaborate a little?
Hello Gregg,
I apologize if I have introduced some ambiguity with my previous response. I was trying to elaborate on the mechanism we use for the actual displaying of the value you want to change - by default Sitefinity will always use the UserProfile information, and to be more specific - the Nickname field. You can edit the users' Nicknames and set them to be equal to the FirstName and LastName, so you'll get this value displayed on the forums.
Greetings,
Boyan Barnev
the Telerik team
So our only option is to go into all 1000 user accounts and manually change their nickname to match their real names. And to watch for any new users that sign up and do the same for them? Are you saying that there is no programmatic way for me to pull the users real name rather than their nickname? That makes this CMS seem to be a bit heavy handed.
Hello Gregg,
Thnak you for your feedback.
Please note that as with any other Forums solution, Sitefinity also uses the Nickname field of the user's profile for displaying the forum nickname.
For your conveninece please find below a code sample that will attempt at achieving two tasks:
1. Create a UserProfile for any users that do not have a profile
2. Update the Nickname in the user's profile to match the user's FirstName, or any desired value
//Get the collection of registered providers
var providers = UserManager.GetManager().Providers;
var nickname =
""
;
foreach
(var provider
in
providers)
//Load managers
UserManager customproviderUsersManager = UserManager.GetManager(provider.Name);
customproviderUsersManager.Provider.SuppressSecurityChecks =
true
;
UserProfileManager profileManager = UserProfileManager.GetManager();
profileManager.Provider.SuppressSecurityChecks =
true
;
//Get all users for this provider
var customproviderUsers = customproviderUsersManager.GetUsers();
foreach
(var user
in
customproviderUsers)
if
(user.FirstName.IsNullOrEmpty())
nickname = user.Email;
else
nickname = user.FirstName;
//Load the user profile for each user
var userProfile = profileManager.GetUserProfile(user.Id,
typeof
(SitefinityProfile).FullName);
//If there's no profile, create one
if
(userProfile ==
null
)
SitefinityProfile sfProfile = profileManager.CreateProfile(user, Guid.NewGuid(),
typeof
(SitefinityProfile))
as
SitefinityProfile;
sfProfile.FirstName = user.FirstName;
sfProfile.LastName = user.LastName;
sfProfile.SetValue(
"Nickname"
, nickname);
userProfile.SetValue(
"Nickname"
, nickname);
//Commit changes
profileManager.SaveChanges();
if
(user.FirstName.IsNullOrEmpty())
nickname = user.Email;
else
nickname = user.FirstName;
Boyan,
Thank you for your help. As I mentioned in a previous post I'm new to Sitefinity and still trying to figure out how everything works. I appreciate the code sample. My question now is... Where does this code go? Am I to override some existing method? Does this go in a global.asax method? I apologize if this should be obvious but I'm afraid I don't know where to put this code.
Hello Gregg,
Please do not hesitate to share your concerns with us, that's absolutely fine.
In fact Sitefinity derives its roots from a standard .NET WebApplication, so you can apply prior .NET skills for working with ti as well. For example, the current sample that we've provided can be placed in the code-behind of an ASP.NET WebForm, then build the project and navigate to the WebForm in your browser - the code will be executed automatically.
Kind regards,
Boyan Barnev
the Telerik team