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.
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:
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:
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!
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!