# Modify user profiles

The following example describes how to modify a user profile by changing the first name and last name values.

To modify a profile, you get an instance of the profile. In this example, you get the instance of the profile by the ID of the user. For more information, see [Query user profiles](https://www.progress.com/documentation/sitefinity-cms/for-developers-query-user-profiles). Then, you change the desired values, and finally, you save the changes through the profile manager.

```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 Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.Security.Users
{
    public partial class UserProfilesSnippets
    {
        public static void ModifyUserProfile(Guid userId, string newFirstName, string newLastName)
        {
            UserProfileManager profileManager = UserProfileManager.GetManager();
            UserManager userManager = UserManager.GetManager();

            User user = userManager.GetUser(userId);

            if (user != null)
            {
                SitefinityProfile profile = profileManager.GetUserProfile<SitefinityProfile>(user);

                profile.FirstName = newFirstName;
                profile.LastName = newLastName;

                profileManager.SaveChanges();
            }
        }
    }
}

```

## Modify user profile custom fields

If you have extended the user profile with additional custom fields, you can also get and set values for these fields. Since custom fields cannot be part of the predefined data model for user profiles, they do not appear automatically as properties you can work with. You must use the `GetValue` and `SetValue` extension methods when working with user profile custom fields. For more information on adding custom fields, see [Custom fields](https://www.progress.com/documentation/sitefinity-cms/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 ModifyUserProfileWithCustomFields(Guid userId, string newFirstName, string newLastName, string newJobTitle, decimal newAge)
        {
            UserProfileManager profileManager = UserProfileManager.GetManager();
            UserManager userManager = UserManager.GetManager();

            User user = userManager.GetUser(userId);

            if (user != null)
            {
                SitefinityProfile profile = profileManager.GetUserProfile<SitefinityProfile>(user);

                profile.FirstName = newFirstName;
                profile.LastName = newLastName;

                //Checks whether the user profile contains a JobTitle field and sets its value
                if (profile.DoesFieldExist("JobTitle"))
                {
                    var oldJobTitle = profile.GetValue<string>("JobTitle");
                    profile.SetValue("JobTitle", newJobTitle);
                }

                //Checks whether the user profile contains an Age field and sets its value
                if (profile.DoesFieldExist("Age"))
                {
                    var oldAge = profile.GetValue<decimal>("Age");
                    profile.SetValue("Age", newAge);
                }

                profileManager.SaveChanges();
            }
        }
    }
}

```

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

## Modify custom user profile types

Custom user profile types enable you to group multiple custom fields in their own profile type. For more information about custom profiles, see [Create profile types](https://www.progress.com/documentation/sitefinity-cms/administration-create-profile-types).

When working with custom profile types, you use an object of type `UserProfile`, unlike the previous samples that demonstrate working with the default `SitefinityProfile` type. To get the custom profile, you need to pass the profile type name to the `GetUserProfile` overload of `UserProfileManager`. The name is displayed when you create the custom profile type. If you are not sure about the custom profile type name, it consists of the `Telerik.Sitefinity.Security.Model` namespace and the name of your custom user profile in lowercase. For example, if you have a custom user profile named `AdvancedProfile`, the profile type name for that profile is `Telerik.Sitefinity.Security.Model.advancedprofile`. Once you have an instance of the custom profile in your code, working with the custom profile fields is done in the same way as working with custom fields for the default user profile.
The following sample demonstrates working with custom profiles and their fields:

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

namespace SitefinityWebApp
{
    public class ModifyCustomUserProfile
    {
        public static void ModifyCustomUserProfileFields(Guid userId, string newFirstName, string newLastName, string profileTypeName = null)
        {
            UserProfileManager profileManager = UserProfileManager.GetManager();
            UserManager userManager = UserManager.GetManager();

            User user = userManager.GetUser(userId);

            if (user != null && !profileTypeName.IsNullOrEmpty())
            {
                var profile = profileManager.GetUserProfile(user, profileTypeName);
                if (profile.DoesFieldExist("PreferredLanguage"))
                {
                    var currentValue = profile.GetValue("PreferredLanguage");
                    profile.SetValue("PreferredLanguage", "C#");
                }
            }
            profileManager.SaveChanges();
        }
    }
}
```
