Get user from user profile value

Posted by Community Admin on 04-Aug-2018 08:09

Get user from user profile value

All Replies

Posted by Community Admin on 25-Nov-2011 00:00

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

Posted by Community Admin on 25-Nov-2011 00:00

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

As you can see the custom field is Researcher and the system matches the specific field with the data - John.

Kind regards,
Victor Velev
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 26-Nov-2011 00:00

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

Posted by Community Admin on 28-Nov-2011 00:00

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

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 29-Nov-2011 00:00

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:

Database mapped field does not exist.
Parameter name: methodCallExpression
Actual value was u.Profile.FieldValue



Thanks very much for your help.

Michael

Posted by Community Admin on 30-Nov-2011 00:00

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

You can also consult the following article.

All the best,
Victor Velev
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 11-Apr-2012 00:00

Has there been any advances to this which mean we no longer have to iterate through all the users?

Posted by Community Admin on 23-Dec-2012 00:00

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.

Posted by Community Admin on 26-Dec-2012 00:00

Hello Eric,

The problem is in this expression:

var ReportsString = Telerik.Sitefinity.Model.DataExtensions.GetValue(CurrentUserProfile, "AuthorizedReports");
 In case the field is null, you are trying to assign a null value to var (an implicitly-typed local variable). Since the field from the user profile are accessed at runtime, you get a runtime error, i.e. you do not get an error when you compile the solution, which makes your build successful. Moreover, if the field is not null, it you will not get an error, because you will assign some value to the implicitly-typed local variable (var).


This is how you need to do the null check, so it will work in both cases (null or not-null):
(if Telerik.Sitefinity.Model.DataExtensions.GetValue(CurrentUserProfile, "AuthorizedReports") == null)
    // The rest of the code here - ex: the check for string.Empty

I hope this will help you solve your problem. Do not hesitate to write us in case other issues appear.
All the best,
Ivan
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed