Tuesday, October 11, 2011

Get UserProfile Object of Logged-in User

In this article, I will explain how to get UserProfile object for Logged-in User (current user).
//Function Call
UserProfile objUserProfile= GetProfileOfCurrentUser(SPContext.Current.Web.CurrentUser.LoginName)
public UserProfile GetProfileOfCurrentUser(string accountName)
        {
            UserProfile profile = null;
            try
            {
                if (string.IsNullOrEmpty(accountName))
                {
                    return null;
                }
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPSite site = SPContext.Current.Site;
                    ServerContext context = ServerContext.GetContext(site);

                    UserProfileManager profileManager = new UserProfileManager(context);
                    if (!profileManager.UserExists(accountName))
                    {
                        profile = null;
                    }

                    profile = profileManager.GetUserProfile(accountName);
                });
            }
            catch (Exception ex)
            {
                profile = null;
            }
            return profile;
        }
Share:

0 comments:

About Me

My photo
Based in Bangalore, INDIA, Amit is a MCC. He has an MCA from the U.P.T.U., INDIA and holds Microsoft certifications including SharePoint 2007/2010/2013. He is an contributor to MSDN and http://amitkumarmca04.blogspot.com/. Amit have experience in SharePoint,ASP.Net with C#, Classic ASP, VB,Macros,HTML,JavaScript,JSON,BootStrap and AngularJS.

Followers

Labels