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

Forums / Developing with Sitefinity / Access User Profile Custom Fields

Access User Profile Custom Fields

3 posts, 0 answered
  1. Michael
    Michael avatar
    13 posts
    Registered:
    25 Aug 2008
    26 Sep 2013
    Link to this post
    I am looking for a way to access custom fields in a user profile.  I have created a new User Profile Type called Customer and now have added a custom number field called OfficeID.  I would like to be able to get the user and the values of the custom fields.  The following is the code I have tried based on previous posts, but office returns 0 when the value should be 1.  Any suggestions?

    UserProfileManager profileManager = UserProfileManager.GetManager();
    UserManager userManager = UserManager.GetManager();
      
    var myType = "Telerik.Sitefinity.Security.Model.Customer";

    if (UserProfilesHelper.ProfileTypeExists(myType))

       {
    varprofType = TypeResolutionService.ResolveType(myType);                     
    var profName = UserProfilesHelper.GetUserProfileTypeNames().Where(u => u.Contains("Customer")).SingleOrDefault();
    user = userManager.GetUser(userId);
            var userProfile = profileManager.CreateProfile(user, profName);
    officeID = userProfile.GetValue("OfficeID").ToString();
        }
  2. Svetoslav Manchev
    Svetoslav Manchev avatar
    735 posts
    Registered:
    06 Dec 2016
    30 Sep 2013
    Link to this post
    Hello Michael,

    To get the field value you could use the following method:

    public string GetUserProfileFieldByUserId(Guid userId, UserManager userManager)
            {
                var myType = "Telerik.Sitefinity.Security.Model.customer";
     
                if (UserProfilesHelper.ProfileTypeExists(myType))
                {
     
                    var profileManager = UserProfilesHelper.GetUserProfileManager<SitefinityProfile>();
                    var profile = profileManager.GetUserProfile(userId, myType);
                    var fieldValue = profile.GetValue("OfficeID").ToString();
     
                    return fieldValue;
                }
                return null;
            }
        
    and use it for example with the following code:
    UserManager userManager = UserManager.GetManager();
    var userId = userManager.GetUsers().Where(uid => uid.UserName == "Username").FirstOrDefault().Id;
    var value = GetUserProfileFieldByUserId(userId, userManager);

    To use GetValue and SetValue you need to add the following reference:

    using Telerik.Sitefinity.Model;

    More information could be found in the following article.
    Regards,
    Svetoslav Manchev
    Telerik
    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
    13 posts
    Registered:
    25 Aug 2008
    30 Sep 2013 in reply to Svetoslav Manchev
    Link to this post
    var myType = "Telerik.Sitefinity.Security.Model.customer";

    Thank you for your help.  The error I was making was in the case.  I was using Customer vs. customer which is the name in code.  
3 posts, 0 answered