sharepoint 2010 获取用户信息UserProfile方法

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

Microsoft.Office.Server.dll,

Microsoft.Office.Server.UserProfiles.dll

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这样的方法。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值