SharePoint Tip #20: Get User Profile

User Profiles are a central concept to Office SharePoint Server 2007. They store all the information about the SharePoint users in a central location, the User Profile Store, allowing that information to be indexed and searched. The User Profile store can be populated and synchronized with several data sources:

  • Active Directory directory services
  • LADP Directory (other than Active Directory)
  • Databases and web services, via Business Data Catalog
  • Custom sources, via the User Profiles object model

You can also add users to it manually. Additionally, you can add custom properties to the User Profile and map some of them to the data sources you have. This great flexibility allows a multitude of scenarios. In this post I'll show you how you can get access a User Profile programmatically.

Everything is starts with the UserProfileManager object. Here is how you use it to get a UserProfile object.

// open a site collection
using (SPSite site = new SPSite("http://myportal"))
    // get the server context
    ServerContext context = ServerContext.GetContext(site);

    // create the profile manager object
    UserProfileManager profileManager = new UserProfileManager(context);

    // get the user profile
    UserProfile profile = profileManager.GetUserProfile("domain\\username");

There constructor for the UserProfileManager class is overloaded, which means there are other ways to create an instance but this is the way I usually do it. The GetUserProfile method is also overloaded, allowing you to get a user profile using its ID or just getting the current user's profile.

Attention: User Profiles are a MOSS only feature. You cannot use them with WSS.