Querying user profiles

The most common ways to query a user profiles are:

  • by user’s ID
  • by username

In this examples, you make a query for the specified user and pass it to the GetUserPofile<T> method of the manager.

Querying a profile by user’s ID

public static SitefinityProfile GetUserProfileByUserId(Guid userId)
{
    UserProfileManager profileManager = UserProfileManager.GetManager();
    UserManager userManager = UserManager.GetManager();
 
    User user = userManager.GetUser(userId);
 
    SitefinityProfile profile = null;
 
    if (user != null)
    {
        profile = profileManager.GetUserProfile<SitefinityProfile>(user);
    }
 
    return profile;
} 

Querying a profile by username

The code for querying a user profile by username is to the one for querying user profiles by user’s ID. The only difference is that you query the user by calling the GetUser(string username) overload.

Alternatively, you can make a query for all the profiles and filter them by a desired criteria. You must call the desired overload of the GetUserProfiles method. Note that it returns all available profiles no matter their type. If you have other profile implementations, you must filter the collection by the type of the profile.

Querying a profile by user’s ID

public UserProfile GetUserProfileByUserId(Guid userId)
{
    UserProfileManager profileManager = UserProfileManager.GetManager();
 
    return profileManager.GetUserProfiles(userId).Where(p => p.GetType().FullName == typeof(SitefinityProfile).FullName).SingleOrDefault();
} 

Querying all profiles

public List<SitefinityProfile> GetAllProfiles()
{
    UserProfileManager profileManager = UserProfileManager.GetManager();
 
    return profileManager.GetUserProfiles().Where(p => p.GetType().FullName == typeof(SitefinityProfile).FullName).Cast<SitefinityProfile>().ToList();
}

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK