SharePoint 2010 – Get User Profile Info using Visual Studio 2010 C#

Add the following referrences:
Microsoft.Office.Server
Microsoft.Office.Server.UserProfiles


using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;

namespace CurrentUserNameTitle.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb rootWeb = currentSite.OpenWeb())
{
try
{
string userLoginName = rootWeb.CurrentUser.LoginName;
SPServiceContext serviceContext = SPServiceContext.GetContext(currentSite);
UserProfileManager upm = new UserProfileManager(serviceContext);
UserProfile user = upm.GetUserProfile(userLoginName);
string theDisplayName = user[PropertyConstants.FirstName].ToString() + " " + user[PropertyConstants.LastName].ToString();
string theTitle = user[PropertyConstants.JobTitle].ToString();
lbl1.Text = theDisplayName;
lbl2.Text = theTitle;
} catch(Exception){}
}
}
}
}
}

Advertisements
This entry was posted in SharePoint 2010, Visual Studio 2010 and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s