How to: Retrieve a User Profile

Last modified: January 27, 2010

Applies to: SharePoint Server 2010

This code example shows you how to use the User Profiles object model to retrieve user profile data.

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

  • Microsoft.Office.Server

  • Microsoft.Office.Server.UseProfiles

  • Microsoft.SharePoint

  • System.Web

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;

namespace UserProfilesApp
{
    class Program
    {
        static void Main(string[] args)
        {

            using (SPSite site = new SPSite("http://servername"))
            {
                   SPServiceContext context = 
                       SPServiceContext.GetContext(site);
                  UserProfileManager profileManager = new UserProfileManager(context);
                  string sAccount = "domainname\\username";
                  UserProfile u = profileManager.GetUserProfile(sAccount);
                  //Updates values
                  u[PropertyConstants.HomePhone].Value = "nnnnnnnnnn";
                  u[PropertyConstants.CellPhone].Value = "nnnnnnnnnn";
                  //commits changes
                  u.Commit();

               }
            }

        }
    }
Show: