Get user from user profile value
Hi,
I have to implement a custom login control where the login is based on two user profile custom fields that I have created (MembershipNumber & Postcode).
I'm looking for a way to retrieve a user by the value of a specific key.
I don't really understand the object model for how user profile custom fields are stored, so any info or links woul be good. So far i have this failing piece of code to get a userprofile by the membership number.
userProfileManager.GetUserProfiles().Where(up => up.GetContentLink("MembershipNumber ").DataItem.GetValue<string>("MembershipNumber ") == membershipNumber );
Anywhere in the ball park?
Thanks,
Michael
Hello Michael,
You can use the following code to get specific data from a custom field:
var userManager = UserManager.GetManager();
var user = userManager.GetUser("The user's name");
var pm3 = App.WorkWith().UserProfiles().UserProfiles(user).Where(uP => uP.GetValue<
String
>("Researcher") == "John");
Thanks for the reply.
In my case, I don't know who the user is. I'm trying to identify the user via the values of two different user profile properties (their MemberID and PostCode).
I have been trying to modify your second line of code to something that doesn't require the knowledge of the user but so far with no luck.
On a similar note, is there an administration area where user profiles can be edited. It doesn't seem to be available in the backend admin area under users. Or do I have to roll something myself?
Thanks,
Michael
Hello Michael,
What you can do is to invert the logic of the method. You can get all profiles and loop through the users for specific value in the custom field. Then get all users that match that value.
About the profiles - please check the attached image.
Regards,
Victor Velev
the Telerik team
Hi Victor.
I'm a bit confused. Are you suggesting that my login authentication gets out all users, then iterates through them to find a user with a user profile property "MemberID" that matches the users input? That seems like it won't be very scalable considering there wil be more than 10,000 members.
I've tried a few variations in a single LINQ statement, but they always fail on GetValue<string> with an error:
Hi Michael,
I have prepared another sample for you, however if you need further help in custom development you are free to contact our service partner, so they can consult you on the right way of achieving the desired behaviour.
var manager = UserManager.GetManager();
var profileManager = UserProfileManager.GetManager();
//Get all users
var usersList = manager.GetUsers();
//Create an empty list to store the matching users
var usersMatchList = new List<
User
>();
foreach (User myUser in usersList)
//Load the user profile for each user
var userProfile = profileManager.GetUserProfile(myUser.Id, typeof(SitefinityProfile).FullName);
if (userProfile.GetValue<
String
>(customfield) != null)
if (userProfile.GetValue<
String
>(customfield).Contains(customvalue))
//If so, add the user to the list
usersMatchList.Add(myUser);
Has there been any advances to this which mean we no longer have to iterate through all the users?
Hi,
We are using a custom user profile field. I'm trying to check programmatically if the field is null or empty, unfortunately, I get an error. When the field has a value, everything is fine -- I can retrieve the value. However, when the field does not have a value, a null check returns false, and I cannot check to see if the value is an empty string. Could you please help us?
var profileManager = Telerik.Sitefinity.Security.UserProfileManager.GetManager(); var CurrentUserProfile = profileManager.GetUserProfile(user, typeof(SitefinityProfile).FullName);
var ReportsString = Telerik.Sitefinity.Model.DataExtensions.GetValue(CurrentUserProfile, "AuthorizedReports");
if (ReportsString == null)
//this fails when the field has no value
if (ReportsString.ToString() == String.Empty)
This fails with an error
Thanks.
Hello Eric,
The problem is in this expression:
var ReportsString = Telerik.Sitefinity.Model.DataExtensions.GetValue(CurrentUserProfile,
"AuthorizedReports"
);
(
if
Telerik.Sitefinity.Model.DataExtensions.GetValue(CurrentUserProfile,
"AuthorizedReports"
) ==
null
)
// The rest of the code here - ex: the check for string.Empty