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".

  • Whats the error ? Can you share some code ? – Gautam Sheth Jan 4 '17 at 11:23
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));    

        }
    }
});
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.