Level: 200

Working with Custom User Profile Properties


Ever wanted to extend your Sitecore User Profile?  This short tutorial provides a quick overview of how to set, retrieve, list, and remove custom profile properties using the Sitecore API.

Written by: Joel Watson
Sat, Nov 14 2009

As I’ve used Sitecore, I’ve found the User Manager to be a pretty handy and simple-to-use tool.  Out of the box, Sitecore user profiles have all the basic information you’d expect: username, password, e-mail, etc.  For most instances, this is probably sufficient.  However, I recently came across an instance in which I felt having additional profile properties would be super-useful. Here’s my issue:

I’m developing a new XAML app that will allow logged in Sitecore users to store their Twitter, Facebook, and Flickr account credentials.  Using this information, I’m eventually going to create some sublayouts on the web site-side that will display the specified user’s Twitter feed, Facebook status, and most recent Flickr photos. 

In approaching this, my initial thought was to simply create a new table or two in SQL Server and correlate the new social networking credentials to the users in Sitecore.  Before I started this, however, I decided to crack open the Sitecore API documentation to see if there is any way to extend the User Profile in Sitecore. 

To my great surprise, it turns out that not only can you extend the User Profile to your heart’s content, but even better it’s SUPER simple to do.  In this quick tutorial, I’ll share some code examples that describe exactly how to get, set, remove, and list custom properties on the Sitecore User Profile.



What’s You’ll Need




First Things First: Set up References in Visual Studio

In our C# file, we’re going to need to start by including a few references:


using Sitecore.Security;
using Sitecore.Security.Accounts;


These two references will give us access to all the Sitecore User Profile manipulation goodness that we’ll need for this tutorial. 

Before continuing, it might be a good idea to browse the Sitecore API to see what methods and properties are available through these references (there’s a lot!)—we’ll be focusing only on specific ones from here on out.



Setting the Context: Get the User Profile

Now that we’ve properly included the Sitecore security references, the next thing we’ll want to do is instantiate a UserProfile object based on the currently logged in user (remember, all of this is hypothetically happening in the context of a Sitecore desktop XAML app…).  We can do this simply with the following:



UserProfile curUserProfile = User.Current.Profile;


You can, of course, use “FromName(…)” to accomplish the same thing. However, because our app is directed at logged in users, we’ll use the handy “Current” method.


Okay, now that we’ve got a UserProfile to work with, we can start messing around with custom profile properties.



Setting a Custom User Profile Property

Let’s say that we want to add a new custom property to our User Profile named “TwitterUsername” – just as it sounds, we’ll use this property to store the Twitter username of our Sitecore user.


Setting the custom property is pretty simple:


curUserProfile.SetCustomProperty(“TwitterUsername”, “existdissolve”);


Now there’s a bit of a “gotcha” right here.  Even though we’ve technically “set” the custom property, it hasn’t actually saved the Twitter username to the user’s profile.  As soon as we reload the User Profile, we would find that our custom property no longer exists.  To actually save the custom profile property, we have to do the following:




Not a big deal, but easy to overlook  :)!



Retrieving a Custom User Profile Property

Now that we’ve successfully created and set a custom property on our Sitecore User Profile, we’ll probably want to be able to access that property at some point.  Like setting the property, it’s just as easy to get it back:




Removing a Custom User Profile Property

If we decide that we don’t need this custom profile property anymore, we can go ahead and get rid of it: 


And as with setting the custom property, we’ll need to use the “Save” method again to commit the changes:



Listing Custom User Profile Properties

If you’re like me, the idea of adding custom profile properties might send you into a custom-profile-property-setting frenzy.  So how can you keep track of all the custom properties that you’ve created?  Pretty easily:




This will return the custom profile properties “keys”—in other words, a list of all the custom stuff you’ve set! 


Wrapping Up

As we’ve seen, playing around with custom profile properties in Sitecore is super simple and, depending on what you’re doing, can be a pretty handy thing to do.  Of course, you’ll want to be a bit more thorough in your working out of these approaches in real-life; nonetheless, I hope these brief examples are a useful starting point.  Happy coding!


Please rate this article

5 rates / 4,6 avg.

10 responses to "Working with Custom User Profile Properties"

Useful info thanks! Joel....
Posted: Tuesday, February 02, 2010 6:47 AM
This is my first project with Sitecore. I have been tasked with extracting user, role and profile data from Sitecore. I am having trouble iterating through a list of users and their profiles then parsing the values in PropertyNames, ProperyValuesString and PropertyValuesBinary. I need to do this in order to populate my own tables to build custom reports in SQL Server Reporting Services. I also need to do the same thing with content/data. I have searched high and low and not finding many examples. This site seems to come close, but only working with the currently logged in user. Can you point me to some resources or a snippet that iterates through a UserList?
Posted: Sunday, April 04, 2010 1:40 AM
I tried this code:
Dim UserList As New Sitecore.Security.Accounts.UserList
UserList = Sitecore.Security.Accounts.UserManager.GetUsers

but it throws this error:
Unable to cast object of type 'Sitecore.Common.Filterable`1[Sitecore.Security.Accounts.User]' to type 'Sitecore.Security.Accounts.UserList'
Posted: Sunday, April 04, 2010 2:03 AM
When trying to .Save() the users profile I get the following asp.net exception. Any thoughts on how to get around this? Thanks in advance!!

"This property cannot be set for anonymous users."
[ProviderException: This property cannot be set for anonymous users.]
System.Web.Profile.ProfileBase.SetInternal(String propertyName, Object value) +2442127
System.Web.Profile.ProfileBase.set_Item(String propertyName, Object value) +71
System.Web.Profile.ProfileBase.SetPropertyValue(String propertyName, Object propertyValue) +10
Sitecore.Security.UserProfile.SetPropertyValueCore(String propertyName, Object value) +56
Sitecore.Security.UserProfile.set_SerializedData(Object value) +17
Sitecore.Security.UserProfile.SerializeCustomProperties() +50
Sitecore.Security.UserProfile.Save() +21
Posted: Sunday, November 14, 2010 11:36 PM
Nevermind, issue was I was trying to update users profile (using a validation key in email link) before the user logged in. Changed validation process to require login and it works now.
Posted: Tuesday, November 16, 2010 8:38 PM
I have one custom property called Organization , I need to display the Custom property Data for each respective user in User Manager Page, Where the other info get displayed such as User name, comments etc. I need to add another column.

Any help regarding this??

Posted: Friday, May 11, 2012 2:30 PM
Does not work. Property can not be retrieved next time I access the site.
Posted: Thursday, October 03, 2013 1:25 PM
Sorry it did work but I had to be logged in as the user.
Posted: Thursday, October 03, 2013 2:13 PM
This does not work when updating a user outside of the context of the user being logged into the site. I'm syncing user data from another system via an api and I get the "This property cannot be set for anonymous users." error
Posted: Wednesday, September 03, 2014 1:15 PM
I'm storing users password in SetCustomProperty(“userpassword”, “mypassword”); at the time of user successful login.

1) Is this secure ?
2) Can this be hacked from browser ?
Posted: Friday, November 06, 2015 8:56 AM

Leave a reply

Notify me of follow-up comments via email.