main banner

Content Management

Adding Custom Fields to Sitecore User Part 2

Now its time to cover the access of customized fields using the Sitecore’s API; we will see how a couple of classes help to set, read or even remove the fields in user profile.

Regardless the need that pushes you to add new fields to Sitecore users, you can rely in Sitecore User Profile Templates to implement your adjustments. In a previous post (Part 1 of this topic) the user profile template was extended to handle customized fields through User Management. Now its time to cover the access of customized fields using the Sitecore’s API; we will see how a couple of classes help to set, read or even remove the fields in user profile. Having this features available from the code improves user experience and support content integrity thank to the validation routines that can be implemented with Sitecore’s API.


Accessing the User Profile properties via Sitecore’s API


Sitecore provides APIs classes to access and manipulate the user profile’s fields implementing .NET classes. This way the basic fields as well as any added fields are available to the developer to read the fields value or set new ones.


The Sitecore.Security namespace can be referenced from Sitecore’s API. This namespace houses two important references:

  • Sitecore.Security.Accounts.
    • Access to User object to handle user instances.
  • Sitecore.Security.UserProfile.
    • Allows you to work with the User.Properties

The following snippet is an example of accessing user’s properties.


//Get current user from Sitecore’s context

Sitecore.Security.Accounts.User currentUser = Sitecore.Context.User;

Sitecore.Security.UserProfile currentProfile = currentUser.Profile;

string oldEmail = currentProfile.Email;

currentProfile.Email = “username@domain.com”;

currentProfile.FullName = “Demo User”;

//Save the changes

currentProfile.Save();


Remember calling the Sitecore.Security.UserProfile.Save() method after the updating the properties values.


If you are attempting to update user’s profile you must be aware that it’s only permitted to set properties for authenticated users. You must retrieve an authenticated used before setting fields values and the FromName method can help you with this task. If the user is not authenticated the process must transfer the user to the login page for authentication... this is why most of the time the current context’s user will be used, always validating the user is authenticated before editing it’s properties.


// When the second parameter in FromName method is true only authenticated users are returned.

Sitecore.Security.Accounts.User authenticatedUser =

Sitecore.Security.Accounts.User.FromName(@”domain\user”, true);

if(authenticatedUser != null)

{

Sitecore.Security.UserProfile currentProfile = authenticatedUser.Profile;

//Do something with the profile...

}


The same UserProfile class is useful to handle custom fields from user’s profile. The names of the custom fields can be retrieved using the GetCustomPropertyNames() method; meanwhile the methods GetCustomProperty() and SetCustomProperty() retrieves the custom property instance by its name and sets a value for a given custom property name respectively.


//Get current user from Sitecore’s context

Sitecore.Security.Accounts.User currentUser = Sitecore.Context.User;

Sitecore.Security.UserProfile currentProfile = currentUser.Profile;


//Get all Custom Property names

string[] customPropertyNames = currentProfile.GetCustomPropertyNames();

foreach(string attributeName in customPropertyNames )

{

string attributeValue = currentProfile.GetCustomProperty(attributeName);

//Do something with the attrinute’s value

}


//Modify a Custom Property

if(customPropertyNames.Contains(“IsEnrolled”))

{

//Custom attributes can be retrieved directly from the attributes collection

string IsUsedEnrolledOriginalValue = currentProfile[“IsEnrolled”];


currentProfile.SetCustomProperty(“IsEnrolled”, “false”);

currentProfile.Save();


//The same result will be accomplished using:

//currentProfile[“IsEnrolled”] = “false”;

}


The snippet above can help you to implement validation methods, but doing this required additional work to build the logic to notify the user  about missing fields and this feature is out of the scope of current post. For the meantime you can practice using these classes in scheduled tasks, reports or even page customization based on the user’s preferences.
Luis G.

An expert we all come to when in doubt, Luis was born in Monterrey and has a B.S. Electronic Systems Engineering degree His multiple abilities are backed 10 years of experience in .NET, SQL and recently joined the ranks of front end developers. A Star Wars and Lego enthusiast, he is also a loving father and a very friendly co-worker who writes for the Inflection Point’s blog with lots of inspiration and fills our blog with descriptions of our work environment that recall us all of the good times.

Articles