Extending the User Profile

2
Comments
3
Votes
Login to vote

In some cases you may have a need to add extended data to the user’s profile. The extended profile data is stored in the CMS_UserSettings table. To add a field to this table, navigate to CMS Site Manager > Development > System tables and click edit on the CMS_UserSettings table. From here you manage the fields on the CMS_UserSettings table just like you would when creating a new document type. For this example we are going to add a field to collect the user’s twitter feed URL. To start we select the last field in the list and click the green + icon to add a new field. Set the following properties:

  • Attribute name = TwitterFeedUrl
  • Attribute type = Text
  • Attribute size = 150
  • Field caption = Twitter Feed Url
  • Field Type = text box

Now click OK to save the new field.

Now you can see and edit the value of this field by navigating to CMS Site Manager > Administration > Users click edit on a user and select the "Custom fields" tab.

Now that this field is in the CMS_UserSettings table you will be able to access its value for the current user using the following code:

CMSContext.CurrentUser.GetValue("TwitterFeedUrl");

And we can update the current user’s twitter feed URL with the following code:

UserInfo user = CMSContext.CurrentUser;
user.SetValue("TwitterFeedUrl", "http://twitter.com/kenticosolution");
UserInfoProvider.SetUserInfo(user);

Although these methods work fine for retrieving and setting our custom field’s value, it is not good practice to use strings in our code. One way we can alleviate the need to use strings throughout our code is to take advantage of extension methods. Using extension methods will allow us to add our own custom methods to the UserInfo object like in the code below.

using CMS.SiteProvider;

public static class Extensions
{
    public static string GetTwitterFeedUrl(this UserInfo userInfo)
    {
        return userInfo.GetValue("TwitterFeedUrl") as string;
    }
    public static void SetTwitterFeedUrl(this UserInfo userInfo, string twitterFeedUrl)
    {
        userInfo.SetValue("TwitterFeedUrl", twitterFeedUrl);
    }
}

Now to retrieve the user’s twitter feed URL we can use the following code:

CMSContext.CurrentUser.GetTwitterFeedUrl();

And to set the value we would use the following:

UserInfo user = CMSContext.CurrentUser;
user.SetTwitterFeedUrl("http://twitter.com/kenticosolution");
UserInfoProvider.SetUserInfo(user);

Something else you might want to do with your new field is to add it to the registration form. To do this, navigate to CMS Site Manager - Development - System tables and click edit on the CMS_User table. Select the "Alternative forms" tab and click edit on the "Registration form". Now select the "Layout" tab. From here, you will be able to add your new field to the registration form.

You can also find more information on creating custom registration forms here:
http://devnet.kentico.com/docs/devguide/index.html?creating_a_custom_registration_form.htm

 
Posted by Ryan Williams on 10/22/2010 4:30:08 PM
  
Comments
Ryan Williams
If you have a list of books for the user to choose from, you will need to set up a many-to-many relationship between users and books. The first step will be to create a custom table in Kentico to store the list of books. Next, add a 'favorite books' field to the User Settings table and select 'Text' for the Attribute Type and 'Multiple choice' for the Form Control. For the Data Source option, choose 'SQL Query' and write a select query from your custom table such as (SELECT ID, Title FROM Custom_Books). Kentico will store the id values of the selected books in this field separated by a pipe character (|).

If you are going to let the user type in their favorite books, you will most likely need to create a custom form control (http://devnet.kentico.com/docs/devguide/index.html?form_controls_overview.htm) that will parse the user input and either store the data as delimiter-separated values in a single field on the User Settings table or store the values in a separate table.

For more advanced many-to-many relationships in Kentico, take a look at Jason Ellison's post http://www.kenticosolutions.com/Developer-Tips/Tip/May-2011/Many-to-Many-relationships-in-the-Kentico-CMS-Cont.aspx.
10/30/2012 3:47:06 PM

TheCriticalPath
How might one go about adding a table of data to the user profile? For instance I want users to list their favorite books.
10/30/2012 3:02:18 PM