User department not displaying when reading from user profile using code

%3CLINGO-SUB%20id%3D%22lingo-sub-2135578%22%20slang%3D%22en-US%22%3EUser%20department%20not%20displaying%20when%20reading%20from%20user%20profile%20using%20code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2135578%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20facing%20this%20issue%20where%20the%20following%20code%20is%20not%20working%20for%20one%20user%20(there%20may%20be%20more)%20i.e.%20Department%20field%20is%20coming%20as%20blank%20while%20for%20others%20it%20is%20working.%20If%20you%20see%20screenshots%20below%2C%20I%20am%20showing%20two%20users%3B%20user1%20and%20user2%20and%20for%20user1%20it%20is%20working%20fine%20i.e.%20Department%20is%20being%20displayed%20but%20for%20user2%20not%20Department%20is%20being%20displayed%20i.e.%20it%20is%20blank.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3ECode%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3Eusing%20(SPSite%20site%20%3D%20new%20SPSite(SPContext.Current.Site.RootWeb.Url))%3CBR%20%2F%3E%7B%3CBR%20%2F%3Estring%20userName%20%3D%20System.Web.HttpContext.Current.User.Identity.Name.Replace(%220%23.w%7C%22%2C%20%22%22)%3B%3CBR%20%2F%3ESPServiceContext%20context%20%3D%20SPServiceContext.GetContext(site)%3B%3CBR%20%2F%3EUserProfileManager%20profileManager%20%3D%20new%20UserProfileManager(context)%3B%3CBR%20%2F%3EUserProfile%20currentUserProfile%20%3D%20profileManager.GetUserProfile(userName)%3B%3CBR%20%2F%3EtxtDepartment.Text%20%3D%20(string)currentUserProfile%5B%22department%22%5D.Value%3CBR%20%2F%3E%2F%2Fin%20above%20code%20txtDepartment.txt%20is%20coming%20as%20blank%20for%20user2%20but%20for%20user1%20it%20is%20showing%20dept%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EUser1%20screenshot%20of%20User%20Profile%20Service%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22bXeWZ%22%20style%3D%22width%3A%20667px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F254931i2F20F5FD4654E0CE%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22bXeWZ%22%20alt%3D%22bXeWZ%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EUser2%20screenshot%20of%20User%20Profile%20Service%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%227Wmqy%22%20style%3D%22width%3A%20655px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F254932iE4232EC4EC6C2D0F%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%227Wmqy%22%20alt%3D%227Wmqy%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2135578%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Esharepoint%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Regular Visitor

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