Query user profiles

The most common ways to query a user profile are:

  • by user’s ID
  • by username

In this article, you learn how to query for the specified user and pass it to the GetUserPofile<T> method of the user manager.

Query a profile by user’s ID

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Security.Model;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.Security.Users
{
    public partial class UserProfilesSnippets
    {
        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;
        }
    }
}

Query a profile by username

The code for querying a user profile by username is similar 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 GetUserProfilesmethod. 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.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Security.Model;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.Security.Users
{
    public partial class UserProfilesSnippets
    {
        public static SitefinityProfile GetUserProfileByUserIdAlternative(Guid userId)
        {
            UserProfileManager profileManager = UserProfileManager.GetManager();

            return profileManager.GetUserProfiles(userId).Where(p => p.GetType().FullName == typeof(SitefinityProfile).FullName).SingleOrDefault() as SitefinityProfile;
        }
    }
}

Query all profiles

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Security.Model;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.Security.Users
{
    public partial class UserProfilesSnippets
    {
        public static List<SitefinityProfile> GetAllProfilesNew()
        {
            UserProfileManager profileManager = UserProfileManager.GetManager();

            List<SitefinityProfile> sitefinityProfiles = new List<SitefinityProfile>();

            IQueryable<UserProfile> allProfiles = profileManager.GetUserProfiles();

            foreach (UserProfile profile in allProfiles)
            {
                if (profile.GetType().FullName == typeof(SitefinityProfile).FullName)
                {
                    sitefinityProfiles.Add(profile as SitefinityProfile);
                }
            }

            return sitefinityProfiles;
        }
    }
}

Query all profiles by role

C#
using System.Linq;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Security.Model;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.Security.Users
{
    public partial class UserProfilesSnippets
    {
        public static IQueryable<SitefinityProfile> GetProfilesByRole(string roleName)
        {
            // Get the users from the specified role
            var roleManager = RoleManager.GetManager(SecurityConstants.ApplicationRolesProviderName);
            var usersInRole = roleManager.GetUsersInRole(roleName);
            // Get user profiles
            var profileManager = UserProfileManager.GetManager();
            var userProfiles = profileManager
                .GetUserProfiles()
                .Select(p => p as SitefinityProfile);
            // Get all user profiles in the specified role
            var profilesInRole =
                    (from p in userProfiles
                     from u in usersInRole
                     where p.User.Id == u.Id
                     select p);
            return profilesInRole;
        }
    }
}

In the code example above, you get an instance of the role manager class by passing the provider name for the predefined roles that are built in Sitefinity CMS. Using the role manager you get all the users in a specific role. Next, you get an instance of the user profile manager class and get all the user profiles. Finally, you match the user ID's of the user profiles with the user ID's of the users in the specified role and return the result.

Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.