User department not displaying when reading from user profile using code

Copper Contributor

I am facing this issue where the following code is not working for one user (there may be more) i.e. Department field is coming as blank while for others it is working. If you see screenshots below, I am showing two users; user1 and user2 and for user1 it is working fine i.e. Department is being displayed but for user2 not Department is being displayed i.e. it is blank.

 

Code

using (SPSite site = new SPSite(SPContext.Current.Site.RootWeb.Url))
{
string userName = System.Web.HttpContext.Current.User.Identity.Name.Replace("0#.w|", "");
SPServiceContext context = SPServiceContext.GetContext(site);
UserProfileManager profileManager = new UserProfileManager(context);
UserProfile currentUserProfile = profileManager.GetUserProfile(userName);
txtDepartment.Text = (string)currentUserProfile["department"].Value
//in above code txtDepartment.txt is coming as blank for user2 but for user1 it is showing dept
}

 

User1 screenshot of User Profile Service
bXeWZ

 

User2 screenshot of User Profile Service

7Wmqy

0 Replies