Retrieve current user profile in Sharepoint 2007

My last post was about Contact / Feedback webpart for Sharepoint 2007

In this post I am going to show you how to retrieve current user profile and show the properties like Title, First name, Last Name etc.
using Microsoft.Office.Server.UserProfiles.UserProfileManager()

It is a collection of user profile objects used to access user profile data we need to just call the UserProfileManager() class to access a specific user profile

Here I am going to do the same

Retrieve user profile

Retrieve user profile

Download complete source code

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Web.UI.WebControls;

using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.UserProfiles;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace RetrieveUserProfile
{
    [Guid("b95130c6-6bc0-4ce3-8596-778af2321354")]
    public class RetrieveUserProfile : System.Web.UI.WebControls.WebParts.WebPart
    {
        Label oLabelTitle;
        Label oLabelFirstName;
        Label oLabelLastname;
        Label oLabelAboutMe;
        Label oLabelDept;
        Label oLabelSipAdd;
        Label oLabelWorkPhone;
        Label oLabelWorkEmail;
        Label oLabelWebsite;
        Label oLabelResponsibility;

        Label oLabelMessage;

        protected override void CreateChildControls()
        {
            base.CreateChildControls();           

            //Label for Retrieving user profiles
            oLabelTitle = new Label();
            this.Controls.Add(oLabelTitle);

            oLabelFirstName = new Label();
            this.Controls.Add(oLabelFirstName);

            oLabelLastname = new Label();
            this.Controls.Add(oLabelLastname);

            oLabelAboutMe = new Label();
            this.Controls.Add(oLabelAboutMe);

            oLabelDept = new Label();
            this.Controls.Add(oLabelDept);

            oLabelSipAdd = new Label();
            this.Controls.Add(oLabelSipAdd);

            oLabelWorkPhone = new Label();
            this.Controls.Add(oLabelWorkPhone);

            oLabelWorkEmail = new Label();
            this.Controls.Add(oLabelWorkEmail);

            oLabelWebsite = new Label();
            this.Controls.Add(oLabelWebsite);

            oLabelResponsibility = new Label();
            this.Controls.Add(oLabelResponsibility);

            oLabelMessage = new Label();
            this.Controls.Add(oLabelMessage);
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
            try
            {
                SPSite mySite = SPControl.GetContextSite(Context);
                SPWeb myWeb = SPControl.GetContextWeb(Context);
                ServerContext context = ServerContext.GetContext(mySite);
                UserProfileManager myProfileManager = new UserProfileManager(context);
                string CurrentUser = SPContext.Current.Web.CurrentUser.LoginName;
                UserProfile myProfile = myProfileManager.GetUserProfile(CurrentUser);
                if (myProfile[PropertyConstants.Title].Value != null)
                {
                    oLabelTitle.Text = myProfile[PropertyConstants.Title].Value.ToString();
                }
                if (myProfile[PropertyConstants.FirstName].Value != null)
                {
                    oLabelFirstName.Text = myProfile[PropertyConstants.FirstName].Value.ToString();
                }
                if (myProfile[PropertyConstants.LastName].Value != null)
                {
                    oLabelLastname.Text = myProfile[PropertyConstants.LastName].Value.ToString();
                }
                if (myProfile[PropertyConstants.AboutMe].Value != null)
                {
                    oLabelAboutMe.Text = myProfile[PropertyConstants.AboutMe].Value.ToString();
                }
                if (myProfile[PropertyConstants.Department].Value != null)
                {
                    oLabelDept.Text = myProfile[PropertyConstants.Department].Value.ToString();
                }
                if (myProfile[PropertyConstants.SipAddress].Value != null)
                {
                    oLabelSipAdd.Text = myProfile[PropertyConstants.SipAddress].Value.ToString();
                }
                if (myProfile[PropertyConstants.WorkPhone].Value != null)
                {
                    oLabelWorkPhone.Text = myProfile[PropertyConstants.WorkPhone].Value.ToString();
                }
                if (myProfile[PropertyConstants.WorkEmail].Value != null)
                {
                    oLabelWorkEmail.Text = myProfile[PropertyConstants.WorkEmail].Value.ToString();
                }
                if (myProfile[PropertyConstants.WebSite].Value != null)
                {
                    oLabelWebsite.Text = myProfile[PropertyConstants.WebSite].Value.ToString();
                }
                if (myProfile[PropertyConstants.Responsibility].Value != null)
                {
                    oLabelResponsibility.Text = myProfile[PropertyConstants.Responsibility].Value.ToString();
                }
            }
            catch (UserNotFoundException ex)
            {
                oLabelMessage.Text = ex.ToString();
            }
            });