Thursday, May 20, 2010

UserProfileManager - Find out user properties in SharePoint Web Part

For a given user name, if you need to ever required to find out user properties like first name, last name etc, UserProfileManager object can be used. Here is the code. User Profiles are created automatically the first time a user navigates to their MySite. In other words, you cannot guarantee that the system will have a stored user profile even if the account in question is a valid Active Directory account.

ServerContext spServerContext = ServerContext.GetContext(SPContext.Current.Site);
UserProfileManager upm = new UserProfileManager(spServerContext);

if (!upm.UserExists(_userName))
{
//User does not exist
}
else
{
//User exists. So look for more information
UserProfile u = upm.GetUserProfile(_userName);
if ((u["LastName"].Value != null) && (u["FirstName"].Value != null))
{
//Do something with the information
}
}



Technorati Tags: ,,,

No comments:

Post a Comment