Access User Profile Custom Fields

Posted by Community Admin on 04-Aug-2018 22:19

Access User Profile Custom Fields

All Replies

Posted by Community Admin on 26-Sep-2013 00:00

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();
   

Posted by Community Admin on 30-Sep-2013 00:00

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

Posted by Community Admin on 30-Sep-2013 00:00

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.  

This thread is closed