Adding custom attributes to Users
Hi all,
I'd like to add some custom attributes to users that are generated through code.
I need an attribute to store our customer's unique key in to be able to update the users at a later point in time. Their email addresses and user names can change so i need to use a unique key like their personnel id.
Is this possible in any way?
Erik
Hi Erik,
For custom fields we have our extension methods GetValue and SetValue which help retrieve and set the custom field values accordingly. More information could be found in the following article.
Please note that you will need top add:
using
Telerik.Sitefinity.Model;
Hi Stefani,
UserProfiles have those methods.
The Users themselves, however, do not.
Erik
Hi Erik,
We have answered you on the support ticket you have opened and I will share our answer with the community.
Extending the user type is not possible, there is the option to extend the profile for each user that makes a part of the fields of the user. Trough the UI addition of custom fields to the built in sitefintiy profile (you can add different profiles too) is done from "Manage Profile Types" link in Administration->Users (bottom right). To work with user profiles refer to this documentaion.
If you have added new field to sitefintiy profile to access it trough code use the extension methods GetValue() and SetValue() to access and update the field, you will need reference to Telerik.Sitefintiy.Model to use the methods.
using
Telerik.Sitefintiy.Model;
UserProfileManager profileManager = UserProfileManager.GetManager();
UserManager userManager = UserManager.GetManager();
User user = userManager.GetUser(userId);
SitefinityProfile profile =
null
;
if
(user !=
null
)
profile = profileManager.GetUserProfile<SitefinityProfile>(user);
profile.FirstName = newFirstName;
profile.LastName = newLastName;
//this will get the value of your field
var currentValue = profile.GetValue(
"MyNewField"
);
profile.SetValue(
"MyNewField"
.currentValue + 1);
profileManager.SaveChanges();
If you want to create new profile type and work with its field, here is a sample now to resolve the type of the new profile (different than the built in Sitefintiy Profile), here I update a field on custom profile type called Forumer.
using
Telerik.Sitefinity.Model;
using
Telerik.Sitefinity.Utilities.TypeConverters;
UserManager userManager = UserManager.GetManager();
UserProfileManager profileManager = UserProfileManager.GetManager();
var myType =
"Telerik.Sitefinity.Security.Model.forumer"
;
if
(UserProfilesHelper.ProfileTypeExists(myType))
//resolve the type
var profType = TypeResolutionService.ResolveType(myType);
var profName = UserProfilesHelper.GetUserProfileTypeNames().Where(u => u.Contains(
"forumer"
)).SingleOrDefault();
var user = userManager.GetUsers().Where(u => u.UserName ==
"admin3"
).SingleOrDefault();
//Create the profile, it will not throw exception
var userProfile = profileManager.CreateProfile(user, profName);
//set value to the custom profile type field
userProfile.SetValue(
"FieldName"
,
"NewContents"
);
profileManager.SaveChanges();
userManager.SaveChanges();
Hi Stefani,
Thanks for your information.
One last question;
I'm missing insight into how to get the user/userprofile again after creating it.
If I add new user and profile with a custom field like 'PersonnelId', how do I query Sitefinity at a later point to Get this user again? Not based on login name of the user, but based on the PersonnelId of it's profile.
Erik
After a lot of frustration it turns out that you need the 'using Telerik.Sitefinity.Model' to access these extra Get/Set extensions on the UserProfile.
Hello,
To use the methods GetValue and SetValue the following line is mandatory:
using
Telerik.Sitefinity.Model;