How to: Modify User Profile Data

Applies to: SharePoint Server 2010

Modifying user profile data in the user profile store is one of the most common developer tasks. The code example shows you how to modify the user profile data using the object model.

Note

In Microsoft SharePoint Server 2010, you can also update user profile data by using the User Profile Service Web service. For more information, see How to: Use the Web Service to Modify User Profile Data.

Before running the code example, replace domainname, username, and nnnnnnnnnn with actual values. Also add references to the following in your Microsoft Visual Studio project:

  • Microsoft.Office.Server

  • Microsoft.Office.Server.UserProfiles

  • Microsoft.SharePoint

  • System.Web

Example

This example uses the object model to modify some of the user profile properties.

//Updates a user profile
//Creates a user profile. Obtains the property values from the default 
//domain controller or the master connection that is configured on the 
//server
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using System.Web;

namespace UserProfilesApp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (SPSite site = new SPSite("https://servername"))
                {
                    SPServiceContext context = SPServiceContext.GetContext(site);
                    UserProfileManager profileManager = new UserProfileManager(context);
                    string sAccount = "domainname\\username";
                    UserProfile u = profileManager.GetUserProfile(sAccount);
                    u[PropertyConstants.HomePhone].Value = "nnnnnnnnnn";
                    u[PropertyConstants.CellPhone].Value = "nnnnnnnnnn";
                    u.Commit();



                }
            }
            catch (UserNotFoundException exception)
            {
                Console.WriteLine(exception.ToString());
            }

        }
    }


}

See Also

Tasks

How to: Use the Web Service to Modify User Profile Data