More in this section

Forums / Developing with Sitefinity / Adding custom attributes to Users

Adding custom attributes to Users

7 posts, 0 answered
  1. Erik
    Erik avatar
    10 posts
    Registered:
    31 Jul 2012
    11 Feb 2013
    Link to this post

    Hi all,

    I'd like to add some custom attributes to users that are generated through code.
    I need an attribute to store our customer's unique key in to be able to update the users at a later point in time. Their email addresses and user names can change so i need to use a unique key like their personnel id.
    Is this possible in any way?

    Erik

  2. Stefani Tacheva
    Stefani Tacheva avatar
    718 posts
    Registered:
    31 May 2017
    14 Feb 2013
    Link to this post
    Hi Erik,

    For custom fields we have our extension methods GetValue and SetValue which help retrieve and set the custom field values accordingly. More information could be found in the following article.

    Please note that you will need top add:

    using Telerik.Sitefinity.Model;


    Kind regards,
    Stefani Tacheva
    the Telerik team
    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. Erik
    Erik avatar
    10 posts
    Registered:
    31 Jul 2012
    14 Feb 2013 in reply to Stefani Tacheva
    Link to this post

    Hi Stefani,

    UserProfiles have those methods.
    The Users themselves, however, do not.

    Erik

  4. Stefani Tacheva
    Stefani Tacheva avatar
    718 posts
    Registered:
    31 May 2017
    18 Feb 2013
    Link to this post
    Hi Erik,

    We have answered you on the support ticket you have opened and I will share our answer with the community.

    Extending the user type is not possible, there is the option to extend the profile for each user that makes a part of the fields of the user. Trough the UI addition of custom fields to the built in sitefintiy profile (you can add different profiles too) is done from "Manage Profile Types" link in Administration->Users (bottom right). To work with user profiles refer to this documentaion.

    If you have added new field to sitefintiy profile to access it trough code use the extension methods GetValue() and SetValue() to access and update the field, you will need reference to Telerik.Sitefintiy.Model to use the methods.

    using Telerik.Sitefintiy.Model;
     
    UserProfileManager profileManager = UserProfileManager.GetManager();
                UserManager userManager = UserManager.GetManager();
      
                User user = userManager.GetUser(userId);
      
                SitefinityProfile profile = null;
      
                if (user != null)
                {
                    profile = profileManager.GetUserProfile<SitefinityProfile>(user);
      
                    profile.FirstName = newFirstName;
                    profile.LastName = newLastName;
      
                    //this will get the value of your field
                    var currentValue = profile.GetValue("MyNewField");
                    profile.SetValue("MyNewField".currentValue + 1);
      
                    profileManager.SaveChanges();
                }

    If you want to create new profile type and work with its field, here is a sample now to resolve the type of the new profile (different than the built in Sitefintiy Profile), here I update a field on custom profile type called Forumer.

    using Telerik.Sitefinity.Model;
    using Telerik.Sitefinity.Utilities.TypeConverters;
       
    UserManager userManager = UserManager.GetManager();
                UserProfileManager profileManager = UserProfileManager.GetManager();
                var myType = "Telerik.Sitefinity.Security.Model.forumer";
                if (UserProfilesHelper.ProfileTypeExists(myType))
                {
    //resolve the type
                    var profType = TypeResolutionService.ResolveType(myType);
                    var profName = UserProfilesHelper.GetUserProfileTypeNames().Where(u => u.Contains("forumer")).SingleOrDefault();
                    var user = userManager.GetUsers().Where(u => u.UserName == "admin3").SingleOrDefault();
    //Create the profile, it will not throw exception
                    var userProfile = profileManager.CreateProfile(user, profName);
    //set value to the custom profile type field
                    userProfile.SetValue("FieldName", "NewContents");
                    profileManager.SaveChanges();
                    userManager.SaveChanges();
       
                }

    All the best,
    Stefani Tacheva
    the Telerik team
    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
  5. Erik
    Erik avatar
    10 posts
    Registered:
    31 Jul 2012
    25 Feb 2013 in reply to Stefani Tacheva
    Link to this post

    Hi Stefani,

    Thanks for your information.

    One last question;
    I'm missing insight into how to get the user/userprofile again after creating it.
    If I add new user and profile with a custom field like 'PersonnelId', how do I query Sitefinity at a later point to Get this user again? Not based on login name of the user, but based on the PersonnelId of it's profile.

    Erik

  6. Erik
    Erik avatar
    10 posts
    Registered:
    31 Jul 2012
    27 Feb 2013
    Link to this post

    After a lot of frustration it turns out that you need the 'using Telerik.Sitefinity.Model' to access these extra Get/Set extensions on the UserProfile.

  7. Stefani Tacheva
    Stefani Tacheva avatar
    718 posts
    Registered:
    31 May 2017
    27 Feb 2013
    Link to this post
    Hello,

    To use the methods GetValue and SetValue the following line is mandatory:

    using Telerik.Sitefinity.Model;

    as I have mentioned in my first reply.

    Regards,
    Stefani Tacheva
    the Telerik team
    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
7 posts, 0 answered