修改SharePoint平台登录者显示名称

 

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //提升权限,拿系统服务帐号来执行此段代码
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    //获得当前上下文中的登录者名称
                    string loginName = (SPContext.Current.Site).RootWeb.CurrentUser.LoginName.ToString();

                    //提升权限必需在一个新的上下文中执行,需要New一个新的SPSite与SPWeb
                    using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            web.AllowUnsafeUpdates = true;
                            Microsoft.SharePoint.SPList list = web.SiteUserInfoList;
                            Microsoft.SharePoint.SPUser u = web.SiteUsers[loginName];
                            Microsoft.SharePoint.SPListItem it = list.Items.GetItemById(u.ID);

                            string sql = "select ISNULL(DisplayName,'') as DisplayName from aspnet_UsersInfo where UserName='" + UserInfo.SplitStr(loginName) + "'";
                            DataTable dt = CommonExecSql.RetDs(sql, ConnectionEnum.StrConDataBaseUserManage).Tables[0];
                            if (dt.Rows.Count != 0)
                            {
                                string displayName = dt.Rows[0]["DisplayName"].ToString();

                                Label1.Text = "欢迎您," + displayName + "";

                                if (displayName != it["Title"].ToString())
                                {
                                    it["Title"] = dt.Rows[0]["DisplayName"].ToString();
                                    it.Update();
                                    Page.Response.Redirect(Page.Request.Url.ToString());
                                }
                            }

                        }
                    }
                });
            }
        }

 

posted @ 2015-10-12 11:16  Jakin.zhou  阅读(611)  评论(0编辑  收藏  举报