3

How to retrieve user profile properties "department" and "location" for a specific user using Server-side object model.

I am able to get user's DisplayName, email, manager and few more properties but not able to retrieve user's "department" and "location".

Yayati
  • 1,728
  • 4
  • 16
  • 35
Praveena
  • 328
  • 1
  • 6

2 Answers2

2

Try below code:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite siteCollection = new SPSite("http://siteCollectionurl"))
    {
        using (SPWeb web = siteCollection.OpenWeb())
        {
            ServerContext serverContext = ServerContext.GetContext(siteCollection);
            UserProfileManager userProfileMangager = new UserProfileManager(serverContext);

            SPUser spUser = web.EnsureUser("domain\user");

            UserProfile profile = userProfileMangager.GetUserProfile(spUser.LoginName);    
            Console.WriteLine(Convert.ToString(profile["Department"].Value));    
            Console.WriteLine(Convert.ToString(profile["SPS-Location"].Value));    

        }
    }
});
Gautam Sheth
  • 30,881
  • 1
  • 35
  • 62
1

You can try this

if (empProfile.GetProfileValueCollection("Department").Value != null)
{ department = empProfile.GetProfileValueCollection("Department").Value.ToString(); }

if (empProfile.GetProfileValueCollection("SPS-Location").Value != null)
{ userPropertyDescription = empProfile.GetProfileValueCollection("SPS-Location").Value.ToString(); }

Hope this will hepl you!

Ram
  • 2,444
  • 2
  • 19
  • 38