question

FrankMartin-0949 avatar image
0 Votes"
FrankMartin-0949 asked ·

User department not displaying when reading from user profile using C#

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 name
}

User1 screenshot of User Profile Service
bXeWZ.jpg


User2 screenshot of User Profile Service
7Wmqy.jpg


office-sharepoint-server-developmentoffice-sharepoint-server-customization
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AmosWu-MSFT avatar image
0 Votes"
AmosWu-MSFT answered ·

I recommend you to debug the above code to check if the userName is right.


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @FrankMartin-0949,
I am checking to see how things are going there on this issue. Please let us know if you would like further assistance.
You could accept the helpful post as the answer to help other community members find helpful information quickly.
You could also share your own solution here and accept it as the answer, and we can learn from each other.

0 Votes 0 ·