Sitecore users custom profile properties

Sitecore is using the standard .net security framework. This makes it easy to setup custom profiles on users, and to add custom profile properties. To do so, do the following:

STEP 1: ADD A PROFILE TEMPLATE TO SITECORE

Go to the CORE database and create a new template containing the fields you wish to add to a profile. I have added mine at /sitecore/templates/System/Security/CustomProfile but you can put yours anywhere.

Security Template

Security Template

STEP 2: ADD A USER PROFILE BASED ON THE TEMPLATE YOU JUST CREATED

Go to the CORE database and add a new item based on the template you created, below /sitecore/system/Settings/Security/Profiles.

Custom Profile

Custom Profile

Add the new item below the standard “User” profile. Sitecore will automatically base any new user on the first profile in the list.

STEP 3: THE CODE – APPLY THE PROFILE TO A USER

In this example I have created an extranet user which profile needs to be based on my new profile. To apply the profile to my user I need to write the following code:

public void UpdateUserProfile(string userName, string profileID)
{
  Sitecore.Security.Accounts.User user = Sitecore.Security.Accounts.User.FromName(userName, true);
  user.Profile.ProfileItemId = profileID;
  user.Profile.Save();
}

// How to use the method
UpdateUserProfile("extranet\\bp", "{7BA5AA76-582D-4463-BCF3-775508C8624E}"); 

Remember to prefix the user name with the domain name (in this example, bp is a member of the extranet domain). The GUID is the ID of the profile item I created.

The profile is now based on this profile:

Profile is added to the user

Profile is added to the user

STEP 4: THE CODE – APPLY TEXT TO THE CUSTOM PROFILE

To write text on the custom profile, do the following:

public void UpdateUser(string userName, string address, string phone)
{
  Sitecore.Security.Accounts.User user = Sitecore.Security.Accounts.User.FromName(userName, true);
  user.Profile.SetCustomProperty("Address", address);
  user.Profile.SetCustomProperty("Phone", phone);
  user.Profile.Save();
}

// Calling the method:
UpdateUser("extranet\\bp", "Zepperlinerhallen, Islands Brygge 55, 2300 København S", "70 23 33 30");

The text is now added to the profile:

Text is added to the profile

Text is added to the profile

NOTES:

  • You don’t really need to create the profile in Sitecore. This is just so you can see the custom properties in the Sitecore security editor.
  • The Boolean “true” in the call to Sitecore.Security.Accounts.User.FromName(userName, true) authenticates the user, hence allowing you to edit the user and the user profile.
  • If you get the users (members) with System.Web.Security.Membership.GetAllUsers instead, the user (member) will not be authenticated, and you cannot edit the user (member, sigh…)
  • Remember to call Profile.Save(), if you don’t, the profile is not changed.
Advertisement

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in c#, Sitecore 6, Sitecore 7 and tagged , , , , , . Bookmark the permalink.

7 Responses to Sitecore users custom profile properties

  1. Pingback: Editor Password Recovery in Sitecore 8 | Laub plus Co

  2. Pingback: Securing the password recovery experience in Sitecore 8 - Laub plus Co

  3. Pingback: Sitecore User Csv Import Module - Sixteen Pieces of Flair

  4. Pingback: How to Store Custom User Profile Properties in Sitecore – .Net Blog

  5. Pingback: Authenticate External System user’s into Sitecore as Virtual user | Sitecore Tweaks

  6. Pingback: Which of my old Sitecore posts are still valid in Sitecore 9? | Brian Pedersen's Sitecore and .NET Blog

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.