+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Get user from user profile value

Get user from user profile value

9 posts, 0 answered
  1. Michael
    Michael avatar
    6 posts
    Registered:
    07 Apr 2011
    25 Nov 2011
    Link to this post
    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
  2. Victor Velev
    Victor Velev avatar
    1345 posts
    Registered:
    06 Jul 2015
    25 Nov 2011
    Link to this post
    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
  3. Michael
    Michael avatar
    6 posts
    Registered:
    07 Apr 2011
    26 Nov 2011
    Link to this post
    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
  4. Victor Velev
    Victor Velev avatar
    1345 posts
    Registered:
    06 Jul 2015
    28 Nov 2011
    Link to this post
    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
  5. Michael
    Michael avatar
    6 posts
    Registered:
    07 Apr 2011
    29 Nov 2011
    Link to this post
    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
  6. Victor Velev
    Victor Velev avatar
    1345 posts
    Registered:
    06 Jul 2015
    30 Nov 2011
    Link to this post
    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
  7. Nick
    Nick avatar
    113 posts
    Registered:
    21 Oct 2008
    11 Apr 2012
    Link to this post
    Has there been any advances to this which mean we no longer have to iterate through all the users?
  8. Eric
    Eric avatar
    4 posts
    Registered:
    18 Oct 2012
    23 Dec 2012 in reply to Victor Velev
    Link to this post

    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.

  9. Ivan Eftimov
    Ivan Eftimov avatar
    23 posts
    Registered:
    20 Jul 2016
    26 Dec 2012
    Link to this post
    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
9 posts, 0 answered