How to: Retrieve a User Profile

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

Example

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("https://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();

               }
            }

        }
    }

See Also

Tasks

How to: Use the Web Service to Retrieve Profile Data of a User

How to: Retrieve User Profile Properties

How to: Retrieve What's Common Between Two User Profiles

How to: Get Recent User Profile Changes Using the Change Log