sharepoint 2010 获取用户信息UserProfile方法

1.引用dll文件.在vs2010项目中,添加

Microsoft.Office.Server.dll,

Microsoft.Office.Server.UserProfiles.dll

sharepoint 2010 获取用户信息UserProfile方法_第1张图片

2.获取用户登录名,例如cxx\mossadmin

string LoginName=System.Web.HttpContext.Current.User.Identity.Name.ToString();

3.根据用户名获取UserPorfile

        ///<summary>

        ///根据用户名获取UserProfile

        ///</summary>

        ///<param name="loginName"></param>

        ///<returns></returns>

        public static UserProfile GetUserProfileByLoginName(string loginName)

        {

            UserProfile userProfile =null;

            SPSecurity.RunWithElevatedPrivileges(delegate()

            {

                SPSite site = new SPSite(SPContext.Current.Site.Url);

                SPWeb web = site.RootWeb;

                SPUser user = web.EnsureUser(loginName); //当用户不存在时,自动将改用户添加到sharepoint

                ServerContext sc =ServerContext.GetContext(site);

                UserProfileManager profileManager =newUserProfileManager(sc);

                if (profileManager.UserExists(loginName))

                {

                    userProfile = profileManager.GetUserProfile(loginName); //读取用户配置文件

                }

            });

            return userProfile;

        }

4.在页面或者webpart调用该方法

protected void Page_Load(object sender, EventArgs e)

        {

            UserProfile upf = GetUserProfileByLoginName(System.Web.HttpContext.Current.User.Identity.Name.ToString());

            string PictureUrl = "";

            if (upf[PropertyConstants.PictureUrl].Value !=null)

            {

                PictureUrl = upf[PropertyConstants.PictureUrl].Value.ToString();

            }

            else

            {

                PictureUrl = "/_layouts/images/person.gif";

            }

        }

其中PropertyConstants里面有很多UserProfile字段属性,如何通过这些属性获取对应的字段值,就是使用upf[PropertyConstants.PictureUrl].Value这样的方法。

你可能感兴趣的:(SharePoint,获取用户名,UserProfile,读取用户配置文件)