Sitecore Custom User Profile Properties

I’ve found myself using custom user profile properties on a few projects that use Sitecore authentication. I’ve used it to keep track of Sitecore user accounts that require a password reset after forgot password has been called and an email sent to the user. I’ve added custom profile properties to keep track of the last time a page with a specific item template has been called. I would like to share how to do this.

First thing you will need to do is log into the Sitecore Desktop and switch to the Core database. Open the Content Editor and navigate to the /sitecore/templates/System/Security/User node. Now use the builder tab to add the fields of your choice:

scCustUserProp1

In my example, I’ve added “Has Password Reset” to handle one of the examples described above and “Last News View” to handle the other example.

If we now navigate to /sitecore/system/Settings/Security/Profiles/User, we can see that our user profile has these properties.

scCustUserProp2

As a side note, On yet another project in the past, I have created a custom user profile template that inherited from /sitecore/templates/System/Security/User, added my custom properties to my new custom profile template under a Data field grouping, then created a new profile under /sitecore/system/Settings/Security/Profiles. Let’s call it Custom User for this example. Then when I programatically created users, I’ve set their user Profile by calling:

User u = User.FromName("sitecore\\myNewUser", true);
u.Profile.ProfileItemId = "{11111111-1111-1111-1111-111111111111}";
// Item ID String in Core DB of /sitecore/system/Settings/Security/Profiles/Custom User

Now that we have our custom user profile properties, let’s use them. After a user submits their email, an email is sent to the user with a temporary password. I can set the custom property so our application knows that forgot password was triggered.

MembershipUser _user = Membership.GetUser();
Sitecore.Security.Accounts.User securityAccountUser = Sitecore.Security.Accounts.User.FromName(_user.UserName, true);
if (securityAccountUser != null)
{
     // Set the value to "1" since we are using a Sitecore CheckBox Field
     securityAccountUser.Profile.SetCustomProperty("Has Password Reset", "1");
     securityAccountUser.Profile.Save();
}

Once the user logs into my Sitecore web application. I can check if the user has a password reset by calling:

string resetValue = Sitecore.Context.User.Profile.GetCustomProperty("Has Password Reset");
if (resetValue == "1" && Sitecore.Context.User.IsAuthenticated)
{
     // redirect to change password page.
}

Once the user has successfully changed their password, we can then set our custom property back to null:

MembershipUser _user = Membership.GetUser();
Sitecore.Security.Accounts.User securityAccountUser = Sitecore.Security.Accounts.User.FromName(_user.UserName, true);
if (securityAccountUser != null)
{
     securityAccountUser.Profile.SetCustomProperty("Has Password Reset", null);
     securityAccountUser.Profile.Save();
}

To handle the last view of a news page, I used the Profile SetCustomProperty and GetCustomProperty to save a query string that I convert back and forth from a NameValueCollection to keep the last time they visited that Item. The Item ID is the key and the value is the time the page was visited.

Advertisements

2 Responses to “Sitecore Custom User Profile Properties”

  1. Error while converting Sitecore Virtual user to real user | Sitecore basics! Says:

    […] address]. Then after that let’s say – User tried to create account and we had some custom profile fields. Which were not getting saved. And those fields were critical for us. Because our Login logic uses […]


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: