1

I'm implementing an API where I need to return a user's info from its Profile's CustomProperties.

I can't find a way to just get the profile info using the Email. I don't need to retrieve the user by a custom property, just the email.

Not sure what other details I need to provide! This is Sitecore 10.4

Update: we have around 50,000 users, so UserManager.GetUsers().where... would not perform very well.

1
  • Well, they are kind of similar and may even share an answer (but not necessarily), but I don't need to retrieve using a custom property, my question is simpler, retrieve using the email. I have added a possible answer which doesn't apply to the other question. Also, I clarified in the question that I need to get by email and not a custom property. Thanks
    – TamerM
    Commented Nov 26 at 20:36

5 Answers 5

1

Another approach would be to go straight to the ASP.Net Membership Provider. You should be able to execute the code below from any SitecoreController.

By Username

Membership.FindUsersByName("sitecore\\your-user-name").Cast<MembershipUser>()
.ToList().ForEach(user =>
{
         var res =  ExtractProperties(user);
});

By Email

Membership.FindUsersByEmail("[email protected]").Cast<MembershipUser>()
.ToList().ForEach(user =>
{
    var res = ExtractProperties(user);
});

Extract Properties From User

static string ExtractProperties(MembershipUser user)
{
    var p = ProfileBase.Create(user.UserName, true);

    string result = string.Empty;
    result += $"FullName:{p["FullName"]} ";
    result += $"UserName:{user.UserName} ";
    result += $"Email:{user.Email} ";
    result += $"ProfileItemId:{p["ProfileItemId"]} ";
    foreach (var item in p["SerializedData"] as Dictionary<string, string>)
    {
        result += $"{item.Key}:{item.Value} ";
    }
    return result;
}

If you look at the aspnet_Profile table in the core database, you will see PropertyNames field. This will show you any additional properties that are being stored. You can then access them using p["some other profile property"] in your code.

Credit to this post that pointed me in the direction:

https://stackoverflow.com/questions/2732337/need-a-quick-way-to-mass-update-propertyvaluesbinary-field-for-all-users

2
  • That actually worked, finally!! Thanks man. I can't believe it took this long to find an answer to such a simple question.
    – TamerM
    Commented Nov 26 at 21:43
  • Glad it worked. Your right, feels like it should have been more straight forward! Commented Nov 26 at 22:27
3

You can get user by email or username like below -

using Sitecore.Security.Accounts;
using Sitecore.Security.Authentication;
using Sitecore.Data.Items;
using System.Linq;

public User GetUserProfile(string usernameOrEmail)
{
    // Try to get the user by username
    User user = User.FromName(usernameOrEmail, false);

    // If user not found by username, try finding by email
    if (user == null)
    {
        user = GetUserByEmail(usernameOrEmail);
    }

    return user;
}
private User GetUserByEmail(string email)
{
    var userManager = Sitecore.Security.Authentication.AuthenticationManager.GetUserManager();
    var user = userManager.GetUsers()
                          .FirstOrDefault(u => u.Email.Equals(email, StringComparison.OrdinalIgnoreCase));

    return user;
}

Then you can get user profile using below code -

public string GetUserProfileInfo(User user)
{
    if (user == null) return null;

    var profile = user.Profile;
    if (profile == null) return null;

    // Retrieve some profile fields
    string firstName = profile.FirstName;
    string lastName = profile.LastName;
    string email = profile.Email;

    return $"User: {firstName} {lastName}, Email: {email}";
}
1
  • UserManager.GetUsers() should work but we have around 50,000 users, so this call become very slow
    – TamerM
    Commented Nov 26 at 13:46
0

Although I'm not happy with this solution, the only way I could do it without letting the user waiting for many seconds is by calling the method that is meant to be used in the User Manager UI in Sitecore.

var user = Sitecore.Context.Domain.GetUsersByEmail(0, 1, email, out var total).FirstOrDefault();

Here I'm asking sitecore to search (presumable from the index) using the email and return only the first result from the first page

If someone comes with a better solution, I'd happily mark it as the accepted solution.

-1

You can use below code snippet to get the user profile by the username or email.

using Sitecore.Security.Accounts;
using System.Linq;

// Fetch User by UserName
string userName = "{{UserName}}";
var user = UserManager.GetUsers().Where(x=> x.LocalName == userName);

if (user != null)
{
   var userProfile = user.FirstOrDefault().Profile;
}

// Fetch User by Email
string userEmail = "{{UserEmail}}";
var user = UserManager.GetUsers().Where(x => x.Profile.Email == userEmail);

if (user != null)
{
   var userProfile = user.FirstOrDefault().Profile;
}

Hope this helps!!! Let me know in case you face any issues.

1
  • Same as other answers: UserManager.GetUsers() should work but we have around 50,000 users, so this call become very slow
    – TamerM
    Commented Nov 26 at 13:47
-1

Here I'm adding sample code where using Sitecore's UserManager to fetch a user by their username or email and retrieve the user's profile.

Fetch User by Username:

using Sitecore.Security.Accounts;
using System.Linq;

// Fetch User by UserName
string userName = "{{UserName}}";
var user = UserManager.GetUsers().FirstOrDefault(x => x.LocalName == userName);

if (user != null)
{
   var userProfile = user.Profile;
}
else
{
   // Handle case where user is not found
   // Example: log or return a meaningful error
}

Fetch User by Email:

using Sitecore.Security.Accounts;
using System.Linq;

// Fetch User by Email
string userEmail = "{{UserEmail}}";
var user = UserManager.GetUsers().FirstOrDefault(x => x.Profile.Email == userEmail);

if (user != null)
{
   var userProfile = user.Profile;
}
else
{
   // Handle case where user is not found
   // Example: log or return a meaningful error
}

Hope this help!!

1
  • Same as other answers: UserManager.GetUsers() should work but we have around 50,000 users, so this call become very slow
    – TamerM
    Commented Nov 26 at 13:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.