User profiles code (Get user details from)

protected void Page_Load(object sender, EventArgs e)  
        {  
            //string CurrentLoginUser;  
    
            try  
            {  
                if (!IsPostBack)  
                {  
                    using (SPSite site = new SPSite(SPContext.Current.Site.Url))  
                    {  
                        using (SPWeb web = site.OpenWeb())  
                        {  
                            SPSecurity.RunWithElevatedPrivileges(delegate()  
                            {  
                                SPServiceContext oSPServiceContext = SPServiceContext.GetContext(site);  
                                UserProfileManager upm = new UserProfileManager(oSPServiceContext);  
                                SPUser spUser = web.CurrentUser;  
                                //UserProfileManager ObjProfMgr = new UserProfileManager(oSPServiceContext);  
    
                                UserProfile profile = upm.GetUserProfile(web.CurrentUser.LoginName);  
                                {  
    
                                    if (profile[PropertyConstants.AccountName].Value != null)  
                                        txtname.Text = profile[PropertyConstants.AccountName].ToString().Split('\\')[1];  
                                    if (profile[PropertyConstants.Manager].Value != null)  
                                        txtsuperior.Text = profile[PropertyConstants.Manager].ToString().Split('\\')[1];  
    
                                }  
                            });  
    
                            txtdatevale.Text = DateTime.Now.ToString();  
                            txtperiod.Text = DateTime.Now.Date.AddMonths(6).ToString();  
                              
                              
                        }  
                    }  
                }  
            }  
            catch (Exception ex)  
            {  
                Page.ClientScript.RegisterClientScriptBlock(typeof(SPAlert), "alert", "<script language=\"javascript\">alert('" + ex.Message + " .')</script>");  
                return;  
    
            }  
        }  
  
  
  
  
private UserProfile GetUserProfile(SPUser sPUser)  
        {  
            using (SPSite site = new SPSite(SPContext.Current.Web.Url))  
            {  
                using (SPWeb web = site.OpenWeb())  
                {  
                    SPServiceContext oSPServiceContext = SPServiceContext.GetContext(site);  
                    UserProfileManager upm = new UserProfileManager(oSPServiceContext);  
    
    
                    UserProfile profile = upm.GetUserProfile(sPUser.LoginName);  
    
                    return profile;  
                }  
            }  
        }  
  

Comments

Popular posts from this blog

My Interview Experience

React Checkbox Control SPFx