Export (0) Print
Expand All

How to: Retrieve a User Profile

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.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("http://servername"))
            {
                   ServerContext context = 
                       ServerContext.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

Community Additions

ADD
Show:
© 2014 Microsoft