Working with Custom User Profile Fields in Sitecore

Extending the standard Sitecore user profile to allow custom profile details to be stored is a common project requirement in Sitecore. This is something I thought about struggled with for quite a while. More specifically, storing string data is not a problem but I struggled with how to store custom profile data in more complex field types like droplink, multilist, treelist, etc.

The root of the problem is that you can only Get/Set data in custom profile fields as string data instead of using the built in Sitecore field methods to manipulate the data like you would for a regular template field.

The easy part is actually creating the new custom profile template with your new fields and then adding a new custom profile item in the core database. Many have already blogged about this process in the past and it is outlined in detail in section 3.5 of the Sitecore Security API Cookbook.

Let’s say you have a custom profile field and want to capture a users phone number. It’s straightforward to store and retrieve the telephone number for a specific user as a string. My problems really began when I wanted to store data in more complex fields like droplink fields and multilist fields. When I started to think about it a bit more I realized that I had no idea how to proceed.

How to Populate Custom Profile List Fields

First of all I was storing all of my lookup lists in the Master/Web DB so I wasn’t sure how to reference those lists of data from the user profiles that are stored in the Core DB. I was stuck on this for a while but finally got a useful tip on the Sitecore SDN forums that pointed me in the right direction. The trick is that you are able to add in settings for the target DB and target datasource in the field source on the custom profile template. Of course you will want to reference the web DB since the master DB will likely not be accessible in your production environment.

Example: databasename=web&datasource=/sitecore/content/Meta Content/Lookup Lists/Countries


This is a good start. The field source parameters above mean that your droplink, multilist, treelist profile fields can be pre-populated with data! So when you navigate to a user in the Sitecore client, you can see the options list for that custom field.


Setting Custom Profile Fields Programmatically

In some cases getting to this point may solve your technical need since new users can be created in the Sitecore backend and you can manually set custom profile details for new users. However, you may need to automate the process of creating new users. For example, let’s say you have a registration form on your site and want to automatically create a new user via the Sitecore API and then also assign custom profile details using the Sitecore API.

If you are dynamically creating new Sitecore users with the Sitecore API then it will be pretty straightforward to add custom profile data if it is only going to be stored as string data.

Here is an example:

UserProfile customProfile = SitecoreUser.Profile;
using (new SecurityDisabler())
    customProfile[fieldName] = stringValue;

But it got a little bit trickier for me when I wanted to dynamically set the value of a droplink field. To figure out how to do this I needed to see how the droplink values were being stored in the custom profile fields. If I were to select Canada from the droplink field, I originally assumed that the raw value would be stored as a text string of “Canada”. But I was pleasantly surprised to see that the raw value stored in the droplink custom profile field was the Item ID. This makes perfect sense but I just wasn’t sure what to expect since the data can only be saved as string data.


I took this as good news because it meant that the data in user profile fields was being stored in the exact same way as regular template fields. The only difference is that we do not have access to the fancy Sitecore methods to manipulate and access field values. This goes for any field type, whether it be a droplink, multilist, treelist, datetime, checkbox field, etc.

So if you want to set a droplink value for a user profile field then you need to save the value as an Item ID in string format. And similarly for a mulitlist or treelist field, you would save a pipe delimited set of item ID’s in string format.

UserProfile customProfile = SitecoreUser.Profile;
using (new SecurityDisabler())
    customProfile[fieldName] = item.ID;

Here is an example for setting a checkbox field:

UserProfile customProfile = SitecoreUser.Profile;
if (!boolValue) return;
using (new SecurityDisabler())
    customProfile[fieldName] = "1";

And here is an example for setting a datetime field:

UserProfile customProfile = SitecoreUser.Profile;
using (new SecurityDisabler())
    customProfile[fieldName] = Sitecore.DateUtil.ToIsoDate(dateTimeValue);

Please add any other examples you have in a comment and I’ll add it to the post. I’m hoping to build up a library for all field types. (Just no time to do it at the moment!)

In Closing

There is definitely more to talk about with custom user profiles but this is definitely a good start. I have to admit that Sitecore leaves a lot to be desired in this area. I’ve used a lot of other CMS’s where user profiles were a lot more user friendly and intuitive to use. Since creating and managing users is such a common project requirement I think it is something worth spending some time on to get right.

At one point I got so frustrated with a client’s feedback regarding user profiles that I actually toyed with (and partially built) a bunch of functionality that would automatically create/remove a corresponding user profile item in an item bucket whenever a new user was created in the Sitecore user manager. But it turned into a bit of a nightmare because you need to use a web service in a production environment in order to create the user profile item and populate fields in the master DB.

If anybody has some interesting ideas or implementations using custom user profiles then I’d love the opportunity to talk through some of the use cases and challenges in order to help improve the user experience.

Posted in Sitecore
One comment on “Working with Custom User Profile Fields in Sitecore
  1. Nona Durham says:

    Very good article. So simple.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s