Retrieving User Profile Properties for the Current User

Retrieving a user’s profile properties can be achieved with the following block:

SPSite site = SPContext.GetContext(HttpContext.Current).Site;
            ServerContext serverContext = ServerContext.GetContext(site);

            UserProfileManager profileManager = new UserProfileManager(serverContext);
            UserProfile currentUserProfile = null;

            if (profileManager.UserExists(System.Web.HttpContext.Current.User.Identity.Name))
                currentUserProfile = profileManager.GetUserProfile(System.Web.HttpContext.Current.User.Identity.Name);
            else
                currentUserProfile = profileManager.CreateUserProfile(System.Web.HttpContext.Current.User.Identity.Name);

            string termsOfUseAnswered = (string)currentUserProfile["TermsOfUseAccepted"].Value;
            string securityQuestion1 = (string)currentUserProfile["SecurityQuestion1"].Value;
            string securityQuestion2 = (string)currentUserProfile["SecurityQuestion2"].Value;
            string securityQuestion3 = (string)currentUserProfile["SecurityQuestion3"].Value;

Note that this buids on Creating Custom Profile Properties Through Code.

Bookmark and Share
Leave a comment

0 Comments.

Leave a Reply


[ Ctrl + Enter ]

Performance Optimization WordPress Plugins by W3 EDGE