Showing posts with label UserProfile. Show all posts
Showing posts with label UserProfile. Show all posts

Saturday, February 2, 2013

Fetch User Profile Property for logged IN user using SharePoint Object Model

In this post, I will explain how to fetch user profile property for logged IN user using SharePoint object model.

To use user profile API we need to have reference to the Microsoft.Office.Server.UserProfiles. You can refer below code snippet to fetch user profile property

Code snippet
try
   {
     string department;
     string email;
     SPServiceContext serviceContext = SPServiceContext.GetContext(SPContext.Current.Site);
 
     UserProfileManager upm = new UserProfileManager(serviceContext);
 
     String domainuserName = String.Format(SPContext.Current.Web.CurrentUser.LoginName);
 
     UserProfile up = upm.GetUserProfile(domainuserName);
 
     department = up.GetProfileValueCollection("Department").Value == null ? String.Empty : (String)up.GetProfileValueCollection("Department").Value;
 
     email = up.GetProfileValueCollection("WorkEmail").Value == null ? String.Empty : (String)up.GetProfileValueCollection("WorkEmail").Value;
 
     lbldepartment.Text = department;
     lblemail.Text= email;
    }
    catch (Exception ex)
      {
        lblError.Text = ex.Message;
      }