SharePoint 2007 - Accessing User Profile properties

For my current project, I have a need to access custom mapped properties of a user profile.  In Office Server 2007, we can create additional profile properties.

We do this in Shared Services Administration by clicking on "User profiles and properties" under "User Profiles and My Sites", and then "Add profile property" under "User Profile Properties":

then

I have gone thru this exercise and added several "custom" properties.  These properties are then mapped to properties in Active Directory (AD) and imported on a schedule.  This import did not work in Beta2, but if anyone is wondering, does in fact work in the Beta2TR.

I have added the "Microsoft.Office.Server" and "Microsoft.Office.Server.UserProfiles" namespaces to my code and cannot seem to find a way to access these values for the currently logged on user.  So, if a value gets stored in one of the many available and unused properties of an AD User, such as "employeeId" using a tool such as "adsiedit.msc" supplied with the Windows Server 2003 Support Tools or by Microsoft Identity Integration Server, how can that property be accessed thru the Office Server 2007 object model if it is mapped to a property in SharePoint, such as BadgeNumber or something like that?

I see an UserProfile object in the SDK but none of the properties seem appropriate for this task.  Is it missing or am I looking in the wrong place.

Well, I just had one of those moments.  I am working on this as I write this post.  Look closely at the "Edit User Profile Property" screen:

There is a "Name" property AND a "Display Name" property.  I had been looking at the wrong property all along as the display name of the property I was interested in was in fact different than the name and in order to access its value you need the name.  At any rate, here is a snippet that will get you the values of the custom properties should you need them.

 

using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

...

// get the BadgeNumber for this user from the profile store
string _badgeNumber = string.Empty;
SPSite _SPSite = null;

try
{
    // TODO: move this to a configuration setting somewhere
    // get a reference to the current site
    _SPSite = new SPSite("http://SITEURL");

    // get a reference to the context of the current site
    ServerContext _ServerContext = ServerContext.GetContext(_SPSite);

    // get a UserProfileManager
    UserProfileManager _UserProfileManager = new UserProfileManager(_ServerContext);

    // get the UserProfile of the logged on user
    UserProfile _CurrentUserUserProfile = _UserProfileManager.GetUserProfile(System.Web.HttpContext.Current.User.Identity.Name);

    _badgeNumber = (string)_CurrentUserUserProfile["badgeNumber "].Value;  // NOT ["Badge Number"], that was my initial mistake, using display name instead of name :)
}
finally
{
    _SPSite.RootWeb.Dispose();
    _SPSite.Dispose();
}

...

I hope someone gets some use out of this.  I sure will!

Published 26 October 2006 08:30 AM by Dan Attis
Filed under:

Comments

No Comments