Create user profiles

The following example describes the creating of a user profile separately from the creating of the user. For more information, see the For developers: Create users example.

In this example, you get the instance of the user that the profile belongs to. To create the profile, you call the CreateProfile method of the manager. Then, you set the desired properties of the profile instance and save the changes.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Security.Model;

namespace SitefinityWebApp
{
   public partial class UserProfilesSnippets
   {
       public static void CreateProfileForUser(string username, string firstName, string lastName)
       {
           UserProfileManager profileManager = UserProfileManager.GetManager();
           UserManager userManager = UserManager.GetManager();

           User user = userManager.GetUsers().Where(u => u.UserName == username).SingleOrDefault();

           if (user != null)
           {
               SitefinityProfile userProfile = profileManager.CreateProfile(user, Guid.NewGuid(), typeof(SitefinityProfile)) as SitefinityProfile;

               userProfile.FirstName = firstName;
               userProfile.LastName = lastName;
               profileManager.RecompileItemUrls(userProfile);

               profileManager.SaveChanges();
           }
       }
   }
}

If you've extended the user profile with additional custom fields, you can also set values for these fields. For more information on adding custom fields, see Overview: Custom fields.

In the following example the user profile has two additional custom fields added:

  • JobTitle
    Short text
  • Age
    Number

C#
using System;
using System.Linq;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Security.Model;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.Security.Users
{
   public partial class UserProfilesSnippets
   {
       public static void CreateProfileForUserWithCustomFields(string username, string firstName, string lastName, string jobTitle, decimal age)
       {
           UserProfileManager profileManager = UserProfileManager.GetManager();
           UserManager userManager = UserManager.GetManager();

           User user = userManager.GetUsers().Where(u => u.UserName == username).SingleOrDefault();

           if (user != null)
           {
               SitefinityProfile userProfile = profileManager.CreateProfile(user, Guid.NewGuid(), typeof(SitefinityProfile)) as SitefinityProfile;

               userProfile.FirstName = firstName;
               userProfile.LastName = lastName;

               //Checks whether the user profile contains a JobTitle field and sets its value
               if (userProfile.DoesFieldExist("JobTitle"))
                   userProfile.SetValue("JobTitle", jobTitle);

               //Checks whether the user profile contains an Age field and sets its value
               if (userProfile.DoesFieldExist("Age"))
                   userProfile.SetValue("Age", age);

               profileManager.SaveChanges();
           }
       }
   }
}

You can use the DoesFieldExistextension method from the Telerik.Sitefinity.Model namespace to check if the user contains a particular field.

Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
New to Sitefinity?