如何:检索用户配置文件

上次修改时间: 2010年1月27日

适用范围: SharePoint Server 2010

此代码示例演示如何使用用户配置文件对象模型来检索用户配置文件数据。

运行本代码示例之前,用实际的值替换 domainname、username 和电话号码 ( nnnnnnnnnn )。此外,还应在 Microsoft Visual Studio 项目中添加对以下项目的引用:

  • 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("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();

               }
            }

        }
    }

请参阅

任务

如何:使用 Web 服务检索用户的配置文件数据

如何:检索用户配置文件属性

如何:检索两个用户配置文件之间的共同之处

如何:使用更改日志获取最新用户配置文件更改