读书人

sharepoint 2010 获取用户信息UserPro

发布时间: 2013-03-22 09:49:50 作者: rapoo

sharepoint 2010 获取用户信息UserProfile方法

1.引用dll文件.在vs2010项目中,添加Microsoft.Office.Server.dll,Microsoft.Office.Server.UserProfiles

sharepoint 2010 获取用户信息UserProfile步骤

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 = new UserProfileManager(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解决方案提供商。

读书人网 >编程

热点推荐