Get User Profile picture by Code

Below is the code to get User Profile Picture from User Profile database using object model.

SPSite siteColl = SPContext.Current.Site;

                    SPServiceContext serviceContext = SPServiceContext.GetContext(siteColl);
                    UserProfileManager userProfileManager = new UserProfileManager(serviceContext);
                    UserProfile profile = null;

                    bool existingUser = userProfileManager.UserExists(SPContext.Current.Web.CurrentUser.LoginName);
                    if (existingUser)
                    {
                        profile = userProfileManager.GetUserProfile(SPContext.Current.Web.CurrentUser.LoginName);
                    }
                    imgProfile.ImageUrl = profile[PropertyConstants.PictureUrl].Value.ToString();
                    lblName.Text = SPContext.Current.Web.CurrentUser.Name;


You can add this to a visual web part and to show profile picture on any page. Don’t forget add reference to Microsoft.Office.Server

Comments

Popular Posts