0

I am trying to get a user's information off of SharePoint. I can see the user's information through the UI when I click their name. I can add them to groups and sites freely. But no matter what I try, I cannot get the user's information through code.

Below is a portion of my code that cannot find the specific user. I can find any other user.

SPUser userInGroup=...; //This is the user
Microsoft.SharePoint.SPServiceContext context = Microsoft.SharePoint.SPServiceContext.GetContext(site);
Microsoft.Office.Server.UserProfiles.UserProfileManager profileManager = new Microsoft.Office.Server.UserProfiles.UserProfileManager(context);
Microsoft.Office.Server.UserProfiles.UserProfile profile = profileManager.GetUserProfile(userInGroup.LoginName);
foreach (var property in profile.Properties)
{
    Console.ForegroundColor = ConsoleColor.White;
    Console.Write("{0}", property.DisplayName, property.Name, profile[property.Name].Value);
    Console.ForegroundColor = ConsoleColor.Cyan;
    Console.Write("[{1}]", property.DisplayName, property.Name, profile[property.Name].Value);
    Console.ForegroundColor = ConsoleColor.White;
    Console.Write(":\t");
    Console.ForegroundColor = ConsoleColor.Green;
    Console.WriteLine("{2}", property.DisplayName, property.Name, profile[property.Name].Value);
}
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("----\t----\t----\t----\t----");
Console.ResetColor();

Am I pulling from AD or SP. It looks like I am pulling from SP, but if I go into User Profile Service Application in Central Admin and search for the user, I can't find anything.

1

To retrieve User Profile information in SharePoint 2010 you should have User Profile Service application configured and activated. Microsoft.Office.Server.UserProfiles.UserProfileManager dll need to reference needed.

SharePoint 2010 object model code to retrieve the data:

using (SPSite site = new SPSite("Site URL"))
{

SPServiceContext context = SPServiceContext.GetContext(site);

UserProfileManager upm = new UserProfileManager(context);

UserProfile profile = upm.GetUserProfile("Domail\\UserName");

String WorkEmail=profile[PropertyConstants.WorkEmail].Value.ToString();

String FirstName = profile[PropertyConstants.FirstName].Value.ToString();

String LastName = profile[PropertyConstants.LastName].Value.ToString();

}
  • If that is all I need, then how come the accounts I want can't be found? – Stoic Developer Jun 20 '14 at 21:09
  • did you try my code and is that work? – Waqas Sarwar MVP Jun 20 '14 at 21:13
  • Your code is the same as my code. Difference is you explicitly pull three properties whereas I pull every property. – Stoic Developer Jun 23 '14 at 14:30

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.